I need to run a query (via
functions.php) and find all posts with a certain tag and add those posts to a category.
Find all posts with tag "car" and add them to category "transportation".
EDIT: STILL NOT WORKING, BUT...
This is what I have so far thanks to @benoti's answer (bellow):
$args = array( 'post_type'=>'post', 'tax_query' => array( 'taxonomy' => 'tag', 'field' => 'slug', 'terms' => 'car', ), ); $posts = get_posts($args); foreach ($posts as $post) : //do stuff $cat_id = 1669; // the ID of category transportation $append = true; // If true, terms will be appended to the object. If false, terms will replace existing terms // make some verif that's better wp_set_object_terms($post->ID, $cat_id, 'category', $append); endforeach;
...but it's not working.
Also, to clarify:
I need to add a category to all posts that contain the "car " tag, but I do not know in what category they (the posts) are in.
How can I safely do this without break my site?