Speed problem on loading, PHP code with multiple queries in loop
-
Hi,
Sorry in advance for my bad english.
I’ve created a code in PHP (in the page \wp-content\themes\my-theme\woocommerce\myaccount\my-account.php) to see every products entered in WooCommerce.Here is my code:
$tous_produits = array();
$args=array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$produit_id = get_the_ID();
$product = new WC_Product( $produit_id );$tous_produits[$produit_id]['title'] = get_the_title();
$tous_produits[$produit_id]['description'] = get_post_field('post_content', $produit_id);
$tous_produits[$produit_id]['Lien_Photo'] = $product->get_attribute( 'Lien_Photo' );
$tous_produits[$produit_id]['Lien_Specsheet'] = $product->get_attribute( 'Lien_Specsheet' );
$tous_produits[$produit_id]['Photometrie'] = $product->get_attribute( 'Photometrie' );
$tous_produits[$produit_id]['ID_Famille'] = $product->get_attribute( 'ID_Famille' );
$tous_produits[$produit_id]['ID_Sous_Famille'] = $product->get_attribute( 'ID_Sous_Famille' );
$tous_produits[$produit_id]['ID_Manufacturier'] = $product->get_attribute( 'ID_Manufacturier' );
$tous_produits[$produit_id]['ID_Marque'] = $product->get_attribute( 'ID_Marque' );
$tous_produits[$produit_id]['ID_Serie'] = $product->get_attribute( 'ID_Serie' );
$tous_produits[$produit_id]['ID_Voltage'] = $product->get_attribute( 'ID_Voltage' );
$tous_produits[$produit_id]['ID_Source'] = $product->get_attribute( 'ID_Source' );
$tous_produits[$produit_id]['ID_Type_de_source'] = $product->get_attribute( 'ID_Type_de_source' );
$tous_produits[$produit_id]['ID_Installation'] = $product->get_attribute( 'ID_Installation' );
$tous_produits[$produit_id]['guide_stock'] = $product->get_attribute( 'ID_GD' );
$tous_produits[$produit_id]['Prix_Budget'] = $product->get_attribute( 'Prix_Budget' );
$tous_produits[$produit_id]['Lumen_emis'] = $product->get_attribute( 'Lumen_emis' );
$tous_produits[$produit_id]['Wattage'] = $product->get_attribute( 'Wattage' );
$tous_produits[$produit_id]['id_es'] = $product->get_attribute( 'ID_ES' );
$tous_produits[$produit_id]['id_dlc'] = $product->get_attribute( 'ID_DLC' );
$tous_produits[$produit_id]['id_dlcp'] = $product->get_attribute( 'ID_DLCP' );
endwhile;
}
wp_reset_query();My problem is a speed problem, the page takes like 10 seconds to load. I have more than 600 products, so the code enters the loop more than 600 times and do multiple queries inside the loop, like get_attributes. The thing is that I need to have the infos of all my products because I have to show advanced filters with all the products attributes that exist.
I’ve optimised my database tables, I haven’t see a significant change in the loading speed.
Is there a way to speed up the loading? Thanks for sharing your ideas! ??
- The topic ‘Speed problem on loading, PHP code with multiple queries in loop’ is closed to new replies.