I’m currently redesigning a site that has 700+ posts, divided into about 150 different categories. Some categories have 2 posts in them, other categories have 70.
Now for every category, I want to add a tag to all the posts in that category.
Does anybody know if there is a way to automatically add a specific tag to all the posts in a category?
Or is the only way going through all the posts individually and adding a tag to each one, one by one?
Any help much appreciated!
A good starting point is (as always) the Codex function reference.
There’s one nice little function that can be used to add tags or categories (or whatever built-in or custom taxonomy) to a post:
wp_set_object_terms();
. Maybewp_set_post_terms();
is more easy to use. Further explanation can be found when you follow the link.There are multiple ways to do this: Bulk adding with fetching all posts (for ex. from a category) with
get_posts();
(or other queries), then looping through all given post objects and adding the needed tags. You could also try to add bulk editing options to your admin UI, but in your case I’d go the easiest way and just write a nice little function that adds your tags to one category and another one to check if it was added. Then proceed to the next category. Writing some custom function that catches all your added tags (true/false) and the check afterwards wouldn’t be much. Dumping this into a nice table on shutdown wouldn’t hurt:When you read through linked codex pages, you’ll see that above code won’t work out of the box and is more like a rough guide. It would be highly appreciated if you can edit my answer with your working code for later readers. Thanks.