WordPress tip: use wp_defer_term_counting for bulk classification for efficiency

If our volume for some articles classification, set up a WordPress will perform wp_update_term_count_now function to calculate the use of the classification number, this function will produce three SQL, if set to 20 articles classification, then can produce 60 SQL, so at the time of batch Settings often produce a large number of SQL requests, cause too many database connections.

At this time, we can use the wp_defer_term_counting function to improve the efficiency of batch classification information. This function allows us to use the number statistics for classification after the batch operation is completed, such as the image collection plug-in I made, and batch classification of images.

// We will not count the usage of classification first
wp_defer_term_counting(true);

foreach($post_ids as $post_id){
	$result	= wp_set_post_terms($post_id, $collection_ids, 'collection', true);

	if(is_wp_error($result)){
		return $result;
	}
}

// All images are set after classification in the use of statistics classification
wp_defer_term_counting(false);

 

Leave a Reply

Your email address will not be published. Required fields are marked *