Speeding Up Bulk Post Creation - wp_insert_post & update_post_meta
I am creating posts (variable products) using (wp_insert_post
) function. An Example, I have 9 colors, 9 sizes. Which makes 9×9 = 81 total variations for 1 product, which is 81 times below function to be executed.
function create_product_variation( $product_id, $variation_data){
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' = $product-get_title(),
'post_name' = 'product-'.$product_id.'-variation',
'post_status' = 'publish',
'post_parent' = $product_id,
'post_type' = 'product_variation',
'guid' = $product-get_permalink()
);
// Creating the product variation
$variation_id = wp_insert_post( $variation_post );
// Get an instance of the WC_Product_Variation object
$variation = new WC_Product_Variation( $variation_id );
// Iterating through the variations attributes
foreach ($variation_data['attributes'] as $attribute = $term_name )
{
//Only have 2 attributes, size and color.
$taxonomy = 'pa_'.$attribute;
update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_name );
}
// Prices
$variation-set_price( $variation_data['regular_price'] );
$variation-set_regular_price( $variation_data['regular_price'] );
$variation-set_image_id($variation_data['variation_thumbnail_id']);
$variation-save(); // Save the data
}
Before running the above code, I encapsulate the loop as below:
$productColors = array("siyah","kirmizi","bordo","haki","beyaz","antrasit","gri-kircilli","sari","lacivert","acik-mavi");
$Sizes = array("5xl","4xl","3xl","xxl","xl","l","m","s","xs");
wp_defer_term_counting( true ); //Speeding Up Bulk Update Tricks
wp_defer_comment_counting( true ); //Speeding Up Bulk Update Tricks
foreach ($Sizes as $size){ //Create each variation
foreach($productColors as $color){
$existingVarId = $wpdb-get_col($wpdb-prepare( "SELECT child.post_id
FROM wp_postmeta AS child
INNER JOIN wp_postmeta AS parent
ON child.post_id = parent.post_id
WHERE child.meta_value = %s and parent.meta_value = %s
and child.post_id in (select id from wp_posts where post_type = 'product_variation' and post_parent = %d)", array( $size,$color,$post_id )));
if(!isset($existingVarId[0]))
{
$varCount++;
if (in_array($size, $oversize))
{
/* SKIP Beyaz - Kırmızı - Oversize*/
switch ($model) {
case "Kadın Tişört": $price = 49;break;
case "Fermuarlı Kapşonlu Sweatshirt":$price = 134;break;
case "Kapşonlu Sweatshirt":$price = 119;break;
case "Sweatshirt":$price = 109;break;
case "Atlet":$price = 49;break;
case "Tişört":$price = 65;break;
}
}
else
{
switch ($model) {
case "Kadın Tişört":$price = 49;break;
case "Fermuarlı Kapşonlu Sweatshirt":$price = 108;break;
case "Kapşonlu Sweatshirt":$price = 94;break;
case "Sweatshirt":$price = 84;break;
case "Atlet":$price = 49;break;
case "Tişört":$price = 49;break;
}
}
$variation_data = array(
'attributes' = array(
'beden' = $size,
'renk' = $color,
),
'regular_price' = $price,
'variation_thumbnail_id' = $productColorsAndIDs[$color],
);
create_product_variation( $post_id, $variation_data);
}
}
}
wp_defer_term_counting( false); //Speeding Up Bulk Update Tricks
wp_defer_comment_counting( false ); //Speeding Up Bulk Update Tricks
The creation process is getting slow everytime, even I am on a fast hosting (SiteGround GoGeek hosting plan.) Above code creates 1 product in 1-2 minutes, which is pretty slow, and most of the time, I get gateway 504 errors while running it.
How can I optimize it to work faster ? Deferring seems not affective at all.
Topic wp-insert-post Wordpress optimization performance
Category Web