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
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
- Scaling
- Constant
- Instructions
- 26–51
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.
Third-party callbacks on 'default_category_post_types' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - optionoption read or write
get_option()called directly - querycontent query
get_post()one call below wp_set_post_categories() - cacheobject cache
wp_cache_get()one call below wp_set_post_categories() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($post_categories) | 26 | get_post_type(), get_post_status(), reset() |
!empty($post_categories) | 27 | get_post_type(), get_post_status(), wp_set_post_terms() |
!empty($post_categories) | 32 | get_post_type(), get_post_status(), reset(), wp_set_post_terms() |
empty($post_categories) && !$post_type | 39 | get_post_type(), get_post_status(), apply_filters(), array_merge(), wp_set_post_terms() |
empty($post_categories) && $post_type | 44–46 | get_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" | 51 | get_post_type(), get_post_status(), apply_filters(), array_merge(), is_object_in_taxonomy(), get_option(), wp_set_post_terms() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 62 | 26–51 | 6 | |
| 8.5 | 62 | 26–51 | 6 | |
| 8.4 | 62 | 26–51 | 6 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 65 | 26–54 | 6 | |
| 8.2 | 65 | 26–54 | 6 | |
| 8.1 | 65 | 26–54 | 6 | |
| 7.4 | 65 | 26–54 | 6 |
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:
- 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
- get_post_type()Retrieves the post type of the current post or of a given post.
- get_post_status()Retrieves the post status based on the post ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- get_option()Retrieves an option value based on an option name.
- wp_set_post_terms()Sets the terms for a post.
Used by · 4
- wp_create_categories()Creates categories for the given post.
- wp_insert_post()Inserts or updates a post in the database.
- wp_set_post_cats()Sets the categories that the post ID belongs to.
- wp_xmlrpc_server::mt_setPostCategories()Sets categories for a post.
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.
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.