Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
            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

share|improve this question
1  
Are you using a custom post type? And what is the difference between custom taxonomy and custom term? –  Rutwick Gangurde Oct 17 '11 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"); –  Coombesy Oct 17 '11 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 Oct 17 '11 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 –  Coombesy Oct 19 '11 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 Oct 20 '11 at 6:10

1 Answer 1

up vote 1 down vote accepted

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 ;)

share|improve this answer
    
select your answer as correct answer if it solves your issue. –  Sisir May 25 '13 at 12:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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