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"
-
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.– sakibmoonCommented 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.– qbrCommented Aug 14, 2014 at 17:04
Add a comment
|
1 Answer
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.
-
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?– qbrCommented 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. Commented Aug 16, 2014 at 3:26