Set terms in a custom post

I have a custom post “artist” and a taxonomy/category “artist category”, and i want to create (by development) a new post and set the category of the artist but it doesn’t work.

I have tested it with this code:

Read More
function createNewPost( $response ){
    global $userMeta;

    $userID = $response->ID;
    $user = new WP_User( $userID );

    $role = $userMeta->getUserRole();
    //$catId = get_cat_ID("dj");

    if( $role = 'artiste' ){ 

        $newPost = array(
          'post_title'    => $user->nickname, 
          'post_content'  => $user->description,
          'post_status'   => 'pending',
          'post_author'   => $userID,
          'post_type'     => 'cpt_artists',
          'tax_input' => array('artist-category' => array('dj')
        ));

        $post_id = wp_insert_post( $newPost );

        wp_set_post_terms( $post_id, array( 'dj'), 'artist-category');

    }

}

But it’s not working.

For the test I have listed all the taxonomy of wordpress and the result is:

category, post_tag, nav_menu, link_category, post_format

Why is my custom taxonomy “artist-category” is not listed here?

Related posts

2 comments

  1. I know this is an old question, but the tax_input array should probably look like this since it appears to be hierarchical like a category:

    'tax_input' => array('artist-category' => array( 3 ) //use the ID of the category, not the name of the category

    From WordPress Codex on wp_set_post_terms

    If you want to enter terms of a hierarchical taxonomy like categories,
    then use IDs. If you want to add non-hierarchical terms like tags,
    then use names.

    Resources:

Comments are closed.