I'm trying to add post to custom terms within a custom taxonomy. Not sure if I'm tackling this right. So far we have the custom taxomony 'warehouse', and the custom term 'Old Stock'.

Problem is that when I try to add the term to the post using the term_id then it creates a new term with the id as a name. Here is the code I'm using:

//add new product
    $id = @wp_insert_post(array(
        'post_title' => $name,
        'post_content' => $desc,
        'post_status' => 'publish',
        'post_parent' => $parent,
        'tax_input' => array(
                'warehouse' => $parent
        ), true);

    //set terms
    wp_set_object_terms($id, array($parent), "warehouse");

As you can see I've tried adding the term_id as 'post_parent' as 'tax_input' and also using 'wp_set_object_terms'

If the term_id was 25, then the above would create a new term called "25"! Instead of sticking the post into the term with an id of 25.

Any help would be appreciated, cheers

  • 1
    Are you using a custom post type? And what is the difference between custom taxonomy and custom term? Commented Oct 17, 2011 at 9:42
  • the taxonomy is the group name and the term is the term. But found a solution, if I casted the parent as int it worked. So changing last line to: wp_set_object_terms($id, (int)$parent, "warehouse");
    – Daithí
    Commented Oct 17, 2011 at 13:05
  • Can you post a var_dump() of the $parent before wp_set_object_terms(), please? Something must be wrong, if it works now, might not work tomorrow... really interested in why it worked by casting to int.
    – soulseekah
    Commented Oct 17, 2011 at 13:46
  • @Soulseekah the $parent was coming straight from $_POST, taken from a jquery post. Could've been a newline character from my js being carried across. The post was being created successfully, just term wasn't been added
    – Daithí
    Commented Oct 19, 2011 at 15:08
  • @Coombsey, great! Please include an explanation of this in your answer, so that people who stumble upon the same problem can understand. Thanks.
    – soulseekah
    Commented Oct 20, 2011 at 6:10

1 Answer 1


When dealing with this, it turned out that the $parent var had to be casted as an int. Looking at the function wp_set_object_terms, it tests the param $parent with is_int(). The var $parent was coming from $_POST which was sent though http post using ajax.

Somewhere along the way, probably on the javascript side, my int $parent was being casted as a string. I'm presuming a new line character or something was being added to the var thus making php think it was a string.

Solution was then to cast the var $parent as an int like so: (int)$parent

So changing the last line to

wp_set_object_terms($id, (int)$parent, "warehouse");

Solved the problem ;)

  • select your answer as correct answer if it solves your issue.
    – Sisir
    Commented May 25, 2013 at 12:19

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.