0

I have a taxonomy called Teams. Now I want to assign different teams to posts based on their tags. For example, all posts with tag "LA Lakers" should be assigned the taxonomy term "LA Lakers"

2
  • It's not really clear what you want. If you have a taxonomy named "Teams", why don't you create a new value "LA Lakers" under that taxonomy and assign that to your posts.
    – sakibmoon
    Commented Aug 14, 2014 at 11:16
  • @sakibmoon that's what my question is. Before I created the taxonomy, there were already many posts in my blog. While they don't have any taxonomy term attached to them, they do have been assigned with tags which are nothing but the team names. Now I want to be able to sort these posts by tags and assign taxonomy terms to them in bulk, not one at a time.
    – qbr
    Commented Aug 14, 2014 at 17:04

1 Answer 1

1

First get all the posts with the tag "LA Lakers". Then add "LA Lakers" as the new taxonomy terms for those posts.

//Get all post with the tag LA Lakers
$query = new WP_Query( 'tag=LA Lakers' );

//Loop through all the posts
while( $query->have_posts() ) {
    $query->the_post();
    //Set "LA Lakers" as taxonomy terms for these posts
    wp_set_object_terms($query->post->ID, array("LA Lakers"), "Teams", true);
}
wp_reset_postdata();

I believe you get the idea. See wp_set_object_terms() to get more idea about the parameters.

2
  • I'm sorry but I'm a wp noob. I was expecting there is some graphical way to do it the dashboard or maybe there are some plugins available? Can you tell me where to execute your piece of code?
    – qbr
    Commented Aug 16, 2014 at 3:15
  • 1
    @qbr This is not the right place for plugin recommendation and I also I don't know any plugins for that. But if you put this code in your functions.php and visit your site once, it should work. After that you can remove this code.
    – sakibmoon
    Commented Aug 16, 2014 at 3:26

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.