Whatever is building the write screen for you to post on the frontend may be including the Categories on the screen, and adding them to $_POST
, but your CPTs still must be registered to use them. I assume the Articles
CPT had this, and you may have missed that step in creating yours.
Check you have registered categories for your CPT
1) be sure your CPT has 'taxonomies' => array( 'category' )
in the $args
array.
Side note: to add the wordpress category taxonomy after a post_type has been registered, you can also use register_taxonomy_for_object_type( $taxonomy, $object_type );
where $taxonomy
is 'category'
and $object_type
is 'your_post_type'
Quick check of $_POST[‘cat’]
2) if all good with the above, be sure the '$new_post'
array is receiving the correct input. If you may have more than one category, be sure $_POST['cat']
is giving you an array, if not create the array outside of the $new_post
declaration and pass it in.
if (is_array($_POST['cat']) {
$category_array = $_POST['cat'];
}
else { //for some reason we have a string
$category_array[] = $_POST['cat'];
}
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'tags_input' => $tags,
'post_status' => 'publish',
'post_category' => $category_array,
'post_type' => $post_type
);
Custom Taxonomy Scenario for Clarification
(since this would have to be added to whatever is generating the post creation screen, I doubt this is the issue, but I’ll add this in for information)
If your category is a taxonomy you registered (i.e. Genre for a Books CPT), $new_post
will need tax_input
as described in wp_insert_post
parameters, as an array just like above with category.
Assuming your $_POST
has a field for 'my_taxonomy'
, that is an array of selected my_taxonomy
options from the write screen, i.e.:
$_POST['my_taxonomy'] = array(
'my_taxonomy' => $entered_value[0],
'my_taxonomy' => $entered_value[1]
)
$new_post
would look something like this:
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'tags_input' => $tags,
'post_status' => 'publish',
'post_tax' => $_POST['my_taxonomy'],
'post_type' => $post_type
);
previous answer regarding post_type
Your custom post type would be getting declared in the post_type line.
'post_type' => $post_type );
You need to edit the value of the variable $post_type
so it is name_of_your_post_type