0

Let's say i have a post with post id "1" and post with post id "2".

And i have a custom taxonomy named "my_taxonomy".

The post with post id "1" has: "term1","term2","term3" selected for the "my_taxonomy" terms.

And the post with post id "2" has: "term3","term4","term5" selected for the "my_taxonomy" terms.

I want to programmatically copy the terms from first post to second so the post with post id "2" will have: "term1","term2","term3" selected for the "my_taxonomy" terms now.

How can i do that the most "short coded way" ?

A note: both posts are a custom post type posts.

I tried:

wp_set_object_terms( '2', wp_get_object_terms( '1', 'my_taxonomy' ), 'my_taxonomy');

Doesn't seem to work.

1

This will work:

wp_set_object_terms( 
    '2', 
    wp_get_object_terms( 
        '1', 
        'my_taxonomy', 
        array("fields"=>"ids") 
    ), 
   'my_taxonomy'
);

Explanation:

I added this: ,array("fields"=>"ids") to the attempt i wrote in the question to make the wp_get_object_terms return array of ids alone (what the wp_set_object_terms want to get.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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