$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 - Add category in post type dynamically|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 - Add category in post type dynamically

matteradmin9PV0评论

i have the code that can add a post in a custom post type but adding the category is not working.

$args = array(
    'post_title' => $title ,
    'post_status'   => 'publish',
    'post_type' => 'mypost' 
);

$post_id = wp_insert_post($args);

$category_ids = array(38,39);
wp_set_post_categories( $post_id, $category_ids, 'category');

i have the code that can add a post in a custom post type but adding the category is not working.

$args = array(
    'post_title' => $title ,
    'post_status'   => 'publish',
    'post_type' => 'mypost' 
);

$post_id = wp_insert_post($args);

$category_ids = array(38,39);
wp_set_post_categories( $post_id, $category_ids, 'category');
Share Improve this question edited Feb 8, 2018 at 12:13 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 8, 2018 at 6:25 Nikko Dela CruzNikko Dela Cruz 1474 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You need to register the custom post type with support for the category taxonomy:

add_action('init', 'cyb_register_post_type');
function cyb_register_post_type() {
    $args = array(
        // All the other args
        'taxonomies'          => array( 'category' ),
    );

    register_post_type( 'my_post_type', $args );
}

Then you can set relationships between the custom post type and the categoy taxonomy as you was doing, but you have to correct the code.

From this:

wp_set_post_categories( $post_id, $category_ids, 'category');

To this (previous categories are deleted and replaced by the new categories):

wp_set_post_categories( $post_id, $category_ids );
// The above line is equivalent to
// wp_set_post_categories( $post_id, $category_ids, false );
// or
// wp_set_post_terms( $post_id, $category_ids, 'category', false );

Or to (previous categories are not deleted, new categories are appended):

wp_set_post_categories( $post_id, $category_ids, true );

You can also register a custom taxonomy and use it for your custom post type:

add_action('init', 'cyb_register_post_type_and_taxonomy');
function cyb_register_post_type_and_taxonomy() {

    $post_type_args = array(
        // All the other args
        'taxonomies'          => array( 'my_custom_taxonomy' ),
    );

    register_post_type( 'my_post_type', $post_type_args );

    $taxonomy_args = array(
          // Arguments for the custom taxonomy
          // See https://developer.wordpress/reference/functions/register_taxonomy/
    );

    register_taxonomy( 'my_custom_taxonomy', 'my_post_type', $args );

}

And then use wp_set_post_terms(), not wp_set_post_categories():

wp_set_post_terms( $post_id, $category_ids, 'my_custom_taxonomy');

As you are doing this for the custom post type, you need to use taxonomy. I will suggest you <?php wp_set_post_terms( $post_id, $terms, $taxonomy, $append ) ?> to use. please refer this link for the detailed use of the function.

Post a comment

comment list (0)

  1. No comments so far