0

I have got array of post ids and I want to add term to each of this post. It could be done by loop over each post id and using wp_set_post_terms, for example

$term_id = 10; // for example
$taxonomy = 'my_custom_taxonomy';
$post_ids = array(70, 68, 65, 64, 63, 21, 20, 19, 9);

foreach ($post_ids as $post_id) {
    // true at end means 'append'
    wp_set_post_terms($post_id, $term_id, $taxonomy, true);
}

but this is not efficient for a lot of posts (I want to do this for over 500 posts and it takes to much time). Is there any faster way to do this task?

  • too much time for the server ? or too much time for the user ? you can try do this by lots with WP Cron – mmm Sep 7 '16 at 8:45
  • To much time for server. Admin clicks on button, it redirects to admin-ajax.php page which do this task based on term_id passed as $_GET parameter. – Krzysztof Grabania Sep 7 '16 at 8:48
  • then you can do it with WP Cron in many times. read this : codex.wordpress.org/Function_Reference/wp_schedule_event – mmm Sep 7 '16 at 10:12
  • i don't want to schedule this task. I wonder if there is some one-line, optimized function to do this, like wp_set_post_terms used with multiple term ids for one post (wp_set_post_terms($post_id, array(1, 2, 3...));) – Krzysztof Grabania Sep 7 '16 at 11:27
  • 1
    MySQL transactions (InnoDB) - Similar as mentioned here – birgire Sep 7 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.