categories - assign array of category to custom post type

admin2025-06-03  4

I tried to assign array of categories to custom post type on creation, so I wrote the following code:

$post_id = wp_insert_post( array(
        'post_content' => '',
        'post_name' => $title_slug,
        'post_title' => $title_slug,
        'post_status' => 'publish',
        'post_type' => 'test',
        'post_author' => 'me',
        'post_excerpt' => '',
    //  'post_date' => $date,
        'post_category' => $categories_id,
    //  'tags_input' => array($tags)
    ));
    //print_r($post_categories );
    print_r($categories_id);
    wp_set_post_categories( $post_id, $categories_id) ;

But it doesn't work no category was affected to the custom post even if the print_r($categories_id) return the following array Array ( [0] => 51 [1] => 52 [2] => 53 [3] => 54 [4] => 55 [5] => 54 ) which is the list of category id.

Any ideas?

I tried to assign array of categories to custom post type on creation, so I wrote the following code:

$post_id = wp_insert_post( array(
        'post_content' => '',
        'post_name' => $title_slug,
        'post_title' => $title_slug,
        'post_status' => 'publish',
        'post_type' => 'test',
        'post_author' => 'me',
        'post_excerpt' => '',
    //  'post_date' => $date,
        'post_category' => $categories_id,
    //  'tags_input' => array($tags)
    ));
    //print_r($post_categories );
    print_r($categories_id);
    wp_set_post_categories( $post_id, $categories_id) ;

But it doesn't work no category was affected to the custom post even if the print_r($categories_id) return the following array Array ( [0] => 51 [1] => 52 [2] => 53 [3] => 54 [4] => 55 [5] => 54 ) which is the list of category id.

Any ideas?

Share Improve this question edited Feb 20, 2019 at 15:34 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Feb 20, 2019 at 14:56 alpha.romeoalpha.romeo 491 gold badge1 silver badge5 bronze badges 5
  • Is the $post_id returning a post id? If there's a problem creating the post, that variable could also be a wp_error. – MikeNGarrett Commented Feb 20, 2019 at 15:24
  • Hi, the post is created well , with the title defined , only categories are not affected to the custom post created. – alpha.romeo Commented Feb 20, 2019 at 15:27
  • Your call to wp_set_post_categories is unnecessary, passing post_category as a parameter should be enough on its own. Where is $categories_id coming from? – Tom J Nowell Commented Feb 20, 2019 at 15:34
  • Also, does your test post type actually support categories? If you don't declare that it supports them, you can't add them – Tom J Nowell Commented Feb 20, 2019 at 15:35
  • my post type support category , the meta box category is visible in the right of custom post edit page , and the $categories_id array is comming from list of wp_create_category('categorie_name') command – alpha.romeo Commented Feb 20, 2019 at 15:39
Add a comment  | 

1 Answer 1

Reset to default 0

Use the tax_input argument like this:

$categories = array(51,52,53,54,55);
$post_id = wp_insert_post( array(
        'post_content' => '',
        'post_name' => $title_slug,
        'post_title' => $title_slug,
        'post_status' => 'publish',
        'post_type' => 'test',
        'post_author' => 'me',
        'post_excerpt' => '',
    //  'post_date' => $date,
        'post_category' => $categories_id,
    //  'tags_input' => array($tags),
        'tax_input' => array(
                         'category' => $categories
                       );
    ));

Happy Coding ;)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748884256a314524.html

最新回复(0)