Take the 2-minute tour ×

Is there a function to add array of post id's to a category,something like this array(2,3,5,6) to put in some category,not looking for update post. Tnx in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Not exactly add array, but you can loop through that array and run wp_set_object_terms() on each post id. Just study function's arguments carefully, because it defaults to overwriting terms, rather that adding.

share|improve this answer
I know for that function,but I was hoping that there is a wp builtin function for putting array of post id's to category.Tnx for response. –  user1147 Nov 3 '10 at 21:41
@user1147 - What do you mean "putting array of post id's to a category?" –  MikeSchinkel Nov 4 '10 at 11:20
@Mike: I understand the question as a request for an efficient method that adds multiple posts, identified by their post ID's, to a given category. Currently you have to loop over the array yourself and call wp_set_object_terms() multiple times. –  Jan Fabry Nov 4 '10 at 13:02
@user1147: If you are talking about a lot of posts, it might be worthwhile to temporarily suspend the update of the term count, by calling wp_defer_term_counting(true) at the beginning of your operation and wp_defer_term_counting(false) at the end. –  Jan Fabry Nov 4 '10 at 13:05
@Jan Fabry - I don't know why that wasn't obvious, thank for clarifying. Maybe I should stay away from WA when I'm bleary eyed and badly in need of sleep. :) –  MikeSchinkel Nov 4 '10 at 20:50

Your Answer


