1

I used this code to post articles on the front end:

//the array of args to be inserted with `wp_insert_post()`:
$new_post = array(
    'post_title'    => $title,
    'post_content'  => $content,
    'tags_input'    => $tags,
    'post_status'   => 'publish',
    'post_category' => array('0',$_POST['cat']),         
    'post_type'     => $post_type 
);

It worked when I was using the post type article but I have created a custom post type and then, can't get the category any more...

What do I have to change with array('0',$_POST['cat']) to get the category from my custom post type?

0

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

  • Hi, thanks for answering quickly ! the var $post_type is already loaded with the name of my custom post type name.... – Nick May 28 '17 at 15:29
  • ah, ok. You need the Category of your CPT... I may have read too hastily. The category in question is the default wordpress category taxonomy, yes? Or have you made a new taxonomy for your new CPT? If the latter, you need to pass a different value and I'll update the answer. – hwl May 28 '17 at 16:44
  • I've updated this for what I'm guessing may be the cause, and then added some other things to look at it not. – hwl May 28 '17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.