Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Do you want to do it via the API, or is it OK to do this with SQL queries? It won't be too hard, just one query to insert the new postmeta values, one to remove the post-category relationships, and maybe one to update the category count. –  Jan Fabry Nov 14 '10 at 18:31
    
I definitely want to use the API. Category counts etc. is not something I want to have to account for in this case. The wp_get_object_terms()/wp_set_object_terms() will already make it pretty easy to manipulate terms, but I was hoping for a one-function solution from core. –  Jeremy Clarke Nov 16 '10 at 0:19
add comment

1 Answer

up vote 4 down vote accepted

Hmmm, can't remember or find fitting function either.

There is wp_set_object_terms() that is used in multiple wrappers like wp_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.

share|improve this answer
    
This is the best solution so far, and the one I will probably use. get_object_terms() to fetch, unset() the one I don't want, then wp_set_object_terms() to re-save the array. Too bad there's no wp_set_object_term() and wp_remove_object_term(). –  Jeremy Clarke Nov 16 '10 at 0:18
    
This is how I've done it in the past. It's pretty clunky and rife for improvement, but I believe it to be the best pattern avail. –  editor Jan 11 '11 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.