1

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:

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?

2 Answers 2

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:

0

you can try something like this: for example your category id is 3 then:

wp_set_post_categories($post_id, array("3"));

for more info

3
  • No working too. I have testing that : "wp_get_post_terms($post_id, 'artist-category', array("fields" => "all"));" and the result is "WP_Error Object ( [errors] => Array ( [invalid_taxonomy] => Array ( [0] => La taxinomie est invalide ) ) [error_data] => Array ( ) )"
    – devwin
    Jan 23, 2014 at 11:57
  • Maybe it's because this script is on the fonction.php file ?
    – devwin
    Jan 23, 2014 at 14:14
  • I found part of the solution. My function is lauch on the function.php file and this file is user before init. So the new problem is i already use a hook for this function like : add_action( 'user_meta_after_user_register', 'createNewPost');
    – devwin
    Jan 24, 2014 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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