I am working on a script to convert all posts in a given category to use a postmeta flag instead (testing of MySQL has shown me that on a site as large as mine this will lead to a meaningful decrease in query time).
When converting posts I want to just fetch all posts in the category, add the postmeta then remove the category, which will let me just reload that function until there are no more posts in the category.
I can’t find a good function for removing a term from a post though. I want to give it the post id and the term taxonomy+ID and have it handled for me.
I am also interested in plugins that can do the conversion for me if anyone knows of one. I couldn’t find any that did cat->postmeta, unlike the opposite which can be handled by Scribu’s plugin.
Hmmm, can’t remember or find fitting function either.
There is
wp_set_object_terms()
that is used in multiple wrappers likewp_set_post_categories()
. It can overwrite categories for a post.So you can get post categories, check for unwanted one and write it back excluding unwanted in that case.
A good alternative to wp_set_object_terms is wp_set_post_terms() if you are dealing with a post object. It has a Boolean 4th parameter that lets you decide whether you will merely add terms or overwrite the existing ones.