wppaste
WordPress

wp_set_post_categories( int $post_id = 0, int[]|int $post_categories = array(), bool $append = false ): array|false|WP_Error

Since
2.1.0
Source
wp-includes/post.php:5854
Sets categories for a post.

Description

If no categories are provided, the default category is used.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_idintoptional
The Post ID. Does not default to the ID of the global $post. Default 0.Default: 0
$post_categoriesint[]|intoptional
List of category IDs, or the ID of a single category.
Default empty array.Default: array()
$appendbooloptional
If true, don't delete existing categories, just add on.
If false, replace the categories with the new categories.Default: false

Return value

array|false|WP_Error
Array of term taxonomy IDs of affected categories. WP_Error or false on failure.

Performance profile

How much work a call to wp_set_post_categories() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
26–51

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.

Plugin surface
1 hook

Third-party callbacks on 'default_category_post_types' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()called directly
  • querycontent queryget_post()one call below wp_set_post_categories()
  • cacheobject cachewp_cache_get()one call below wp_set_post_categories()
  • serializeserialisationmaybe_unserialize()one call below wp_set_post_categories()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_set_post_categories() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($post_categories)26get_post_type(), get_post_status(), reset()
!empty($post_categories)27get_post_type(), get_post_status(), wp_set_post_terms()
!empty($post_categories)32get_post_type(), get_post_status(), reset(), wp_set_post_terms()
empty($post_categories) && !$post_type39get_post_type(), get_post_status(), apply_filters(), array_merge(), wp_set_post_terms()
empty($post_categories) && $post_type44–46get_post_type(), get_post_status(), apply_filters(), array_merge(), is_object_in_taxonomy(), wp_set_post_terms()
empty($post_categories) && $post_type && is_object_in_taxonomy() && $post_status !== "auto-draft"51get_post_type(), get_post_status(), apply_filters(), array_merge(), is_object_in_taxonomy(), get_option(), wp_set_post_terms()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6226–516
8.56226–516
8.46226–5163 fewer instructions than PHP 8.3
8.36526–546
8.26526–546
8.16526–546
7.46526–546

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_set_post_categories() runs, in this order:

  1. apply_filters( default_category_post_types )filterline 5870 (+16 into the body)

    Filters post types (in addition to 'post') that require a default category.

Uses · 6

Used by · 4

Source code

function wp_set_post_categories( $post_id = 0, $post_categories = array(), $append = false ) {	$post_id     = (int) $post_id;	$post_type   = get_post_type( $post_id );	$post_status = get_post_status( $post_id ); 	// If $post_categories isn't already an array, make it one.	$post_categories = (array) $post_categories; 	if ( empty( $post_categories ) ) {		/**		 * Filters post types (in addition to 'post') that require a default category.		 *		 * @since 5.5.0		 *		 * @param string[] $post_types An array of post type names. Default empty array.		 */		$default_category_post_types = apply_filters( 'default_category_post_types', array() ); 		// Regular posts always require a default category.		$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) ); 		if ( in_array( $post_type, $default_category_post_types, true )			&& is_object_in_taxonomy( $post_type, 'category' )			&& 'auto-draft' !== $post_status		) {			$post_categories = array( get_option( 'default_category' ) );			$append          = false;		} else {			$post_categories = array();		}	} elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) {		return true;	} 	return wp_set_post_terms( $post_id, $post_categories, 'category', $append );}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.