$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - assign array of category to custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - assign array of category to custom post type

matteradmin8PV0评论

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 ;)

Post a comment

comment list (0)

  1. No comments so far