i'm facing a weird problem. I have a cutom post type 'cpt_dates' with a taxonomy 'cpt_dates_postIDs'. I need to save as a taxonomy term an ID of another post (from another CPT, freshly inserted with $cpt_dates_post_ID = wp_insert_post).

The thing is I don't understand why. Because in all websites I've seen they said to use intval() because the function seems to wait for a numeric number.

... or I missed something

$cpt_dates_post_ID = wp_insert_post( $dates_insert_args, true );

var_dump($cpt_dates_post_ID); // int(68);


wp_add_object_terms( $cpt_dates_post_ID, strval($another_post_id), 'cpt_dates_postID');


wp_add_object_terms( $cpt_dates_post_ID, 'foo', 'cpt_dates_postID');


wp_add_object_terms( $cpt_dates_post_ID, intval($manifestation_insert_id), 'cpt_dates_postID');

Thank you ! Nicolas.

2 Answers 2



wp_add_object_terms( $cpt_dates_post_ID, intval($manifestation_insert_id), 'cpt_dates_postID');

You're actually saying "Add a cpt_dates_postID term with ID intval($manifestation_insert_id) to the CPT with ID $cpt_dates_post_ID".

That won't work, because intval($manifestation_insert_id) is the ID of:

another post (from another CPT, freshly inserted with [...]

Not the ID of an existing $cpt_dates_post_ID term entry.

The reason why these 2 work:

wp_add_object_terms( $cpt_dates_post_ID, strval($another_post_id), 'cpt_dates_postID');
wp_add_object_terms( $cpt_dates_post_ID, 'foo', 'cpt_dates_postID');

Is because you're passing a string as the 2nd parameter. In that case, WordPress will create a new term of type cpt_dates_postID with name strval($another_post_id) or foo.

Off the top of my mind, you could do this instead:

// Create the term entry
$term_ids = wp_insert_term( strval( $another_post_id ), 'cpt_dates_postID' );

// Assign it to your post
wp_add_object_terms( $cpt_dates_post_ID, $term_ids[0], 'cpt_dates_postID');
  • Thank you for your detailled answer, I'm actually on it, I'll let you inform about the result ! Thank's !
    – nico
    Mar 22 at 22:37
  • In fact, I use "wp_create_term" : Adds a new term to the database ... "if it does not already exist".
    – nico
    Mar 22 at 22:41

Thank you @Ignazio. I ended up with this one, and it works (for now) :

$post_object_id = wp_insert_post( $dates_insert_args, true );

$new_term_or_not = wp_create_term( $manifestation_insert_id, 'cpt_dates_postID' );

if( is_wp_error( $new_term_or_not ) && $new_term_or_not->errors["term_exists"][0] != "") :

    $term_to_inset_id = $new_term_or_not->error_data["term_exists"];

else :

    $term_to_inset_id = $new_term_or_not["term_id"];


wp_add_object_terms( $post_object_id, $term_to_inset_id, 'cpt_dates_postID');

Your Answer

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

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