A WordPress-centric search engine for devs and theme authors



wp_set_post_categories ›

Since2.1.0
Deprecatedn/a
wp_set_post_categories ( $post_ID = 0, $post_categories = array(), $append = false )
Parameters: (3)
  • (int) $post_ID Optional. The Post ID. Does not default to the ID of the global $post. Default 0.
    Required: No
    Default:
  • (array|int) $post_categories Optional. List of categories or ID of category. Default empty array.
    Required: No
    Default: array()
  • (bool) $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories.
    Required: No
    Default: false
Returns:
  • (array|false|WP_Error) Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
Defined at:
Codex:

Set categories for a post.

If the post categories parameter is not set, then the default category is going used.



Source

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 ) ) {
		if ( 'post' == $post_type && '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 );
}