{"id":850,"date":"2012-09-11T14:18:25","date_gmt":"2012-09-12T00:18:25","guid":{"rendered":"http:\/\/mymonkeydo.com\/?p=850"},"modified":"2012-09-11T14:18:25","modified_gmt":"2012-09-12T00:18:25","slug":"wp_insert_post-post_category-problem","status":"publish","type":"post","link":"https:\/\/mymonkeydo.com\/wp_insert_post-post_category-problem\/","title":{"rendered":"wp_insert_post post_category problem"},"content":{"rendered":"
I was wp_insert_post to\u00a0programmatically create a custom post type with a custom category. \u00a0Using the following code did not work however.<\/p>\n
$new_post = array(
\n‘post_title’ => ‘some title’,
\n‘post_content’ => ‘some description’,
\n‘post_status’ => ‘publish’,
\n‘post_author’ => $admin_user->ID,
\n‘post_category’ => array($category->term_id)
\n);
\nwp_insert_post($new_post);<\/p>\n
If I read the wordpress codex better and more thoroughly I probably would have seen that there is another special parameter for custom taxonomies. \u00a0Instead of using ‘post_category’ I should be using ‘tax_input’ for custom taxonomy categories. \u00a0So instead of the ‘post_category’ line, it would look like:<\/p>\n
‘tax_input’ => array($category->taxonomy => array($category->cat_name ));<\/p>\n
If this doesn’t work, like it didn’t for me because I’m using it in a CRON<\/strong>, the user you are using may not have rights to modify the taxonomy. \u00a0in that case, as the docs say, use\u00a0wp_set_object_terms. \u00a0The problem is that wp_insert_post uses current_user_can to detect if the current user can add the taxonomy. \u00a0The issue is documented here:\u00a0http:\/\/core.trac.wordpress.org\/ticket\/19373<\/a><\/p>\n <\/p>\n