wp_insert_post post_category problem


I was wp_insert_post to programmatically create a custom post type with a custom category.  Using the following code did not work however.

$new_post = array(
‘post_title’ => ‘some title’,
‘post_content’ => ‘some description’,
‘post_status’ => ‘publish’,
‘post_author’ => $admin_user->ID,
‘post_category’ => array($category->term_id)


If I read the wordpress codex better and more thoroughly I probably would have seen that there is another special parameter for custom taxonomies.  Instead of using ‘post_category’ I should be using ‘tax_input’ for custom taxonomy categories.  So instead of the ‘post_category’ line, it would look like:

‘tax_input’ => array($category->taxonomy => array($category->cat_name ));

If this doesn’t work, like it didn’t for me because I’m using it in a CRON, the user you are using may not have rights to modify the taxonomy.  in that case, as the docs say, use wp_set_object_terms.  The problem is that wp_insert_post uses current_user_can to detect if the current user can add the taxonomy.  The issue is documented here: http://core.trac.wordpress.org/ticket/19373


source: http://wordpress.org/support/topic/problem-with-wp_insert_post-for-custom-post-type-category-is-not-associated
keywords:  wordpress post_category wp_insert_post

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *