wp_set_post_categories

Definition:
function wp_set_post_categories($post_ID = 0, $post_categories = array() {}

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

Parameters

  • int $post_ID: Post ID.
  • array $post_categories: Optional. List of categories.

Source code

function wp_set_post_categories($post_ID = 0, $post_categories = array()) {

	$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:

	if ( !is_array($post_categories) || empty($post_categories) ) {

		if ( 'post' == $post_type && 'auto-draft' != $post_status )

			$post_categories = array( get_option('default_category') );

		else

			$post_categories = array();

	} else if ( 1 == count($post_categories) && '' == reset($post_categories) ) {

		return true;

	}



	if ( !empty($post_categories) ) {

		$post_categories = array_map('intval', $post_categories);

		$post_categories = array_unique($post_categories);

	}



	return wp_set_object_terms($post_ID, $post_categories, 'category');

}

4111

About these ads

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 196 other followers

%d bloggers like this: