Title: Speed problem on loading, PHP code with multiple queries in loop
Last modified: September 1, 2016

---

# Speed problem on loading, PHP code with multiple queries in loop

 *  Resolved [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/)
 * 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! 🙂
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 15 replies - 1 through 15 (of 15 total)

 *  [jessepearson](https://wordpress.org/support/users/jessepearson/)
 * (@jessepearson)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656733)
 * The query is going to take some time to load due to how many products and attributes
   are being loaded.
 * You could always use multiple Layered Nav widgets instead, they allow you to 
   allow your customers to filter by attributes:
 * [http://cld.wthms.co/lTYB/4W17uPIi](http://cld.wthms.co/lTYB/4W17uPIi)
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656741)
 * My client want something very specific, I can’t use a widget.
 * Is there a way to get all the attributes of a product at the same time, without
   having to call get_attribute 18 times for each one?
 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656747)
 * > My client want something very specific
 * What’s the goal of what you’re trying to achieve? May be an easier way.
 * There is get_attributes: [https://github.com/woothemes/woocommerce/blob/7a2154a37aaaaa3ef75d45588ee3384f1e1b219d/includes/abstracts/abstract-wc-product.php#L1390](https://github.com/woothemes/woocommerce/blob/7a2154a37aaaaa3ef75d45588ee3384f1e1b219d/includes/abstracts/abstract-wc-product.php#L1390)
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656807)
 * Thanks Caleb! Instead of calling get_attribute 19 times, I could call get_attributes
   one time in the loop of each product. It will surely speed up the loading a lot!
   The problem is that the values are empty when I retrieved it with get_attributes!
 * …
    ` [pa_id_famille] => Array ( [name] => pa_id_famille [value] => [position]
   => 11 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 )
 *  [pa_id_sous_famille] => Array
    ( [name] => pa_id_sous_famille [value] => [position]
   => 12 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 )
 *  [pa_id_installation] => Array
    ( [name] => pa_id_installation [value] => [position]
   => 13 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 )
 *  [pa_id_source] => Array
    ( [name] => pa_id_source [value] => [position] => 14[
   is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 )
 * …
 * The thing I’m trying to achieved is an advanced filter (amazon style) which all
   the options are the attributes of the products (ID_Famille, ID_Sous_Famille, 
   ID_Manufacturier, ID_Marque, etc.). I think I don’t have the choice to call all
   the attributes and put it in an array to manipulates it after. Your ideas are
   welcome! 🙂
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656833)
 * I also tried this:
    $attr = get_post_meta( $produit_id, ‘_product_attributes’);
   print_r( $attr );
 * Same thing: all values are empty.
 * And I’m sure I have values in it because when I call the values one by one like
   this:
    $product->get_attribute( ‘ID_Famille’ ) it works.
 * This one works too:
    get_the_terms( $produit_id, ‘pa_id_famille’)
 * But I would really like to get all the attributes with one call, because it will
   help a lot for the loading time.
 * This one only returns the attributes, not the values of it:
    wc_get_attribute_taxonomies()
 * I don’t know what else I can try.
 * Thanks for your help.
 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656843)
 * > The thing I’m trying to achieved is an advanced filter (amazon style) which
   > all the options are the attributes of the products
 * How are the core layered nav widgets falling short for what you need?
 * When I ran the above code in testing, the value wasn’t always empty.
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656868)
 * Thanks for you answer Caleb. I don’t understand your question: How are the core
   layered nav widgets falling short for what you need?
    I don’t think I can use
   a widget to do what I need.
 * I’ll resume my problem…
 * When I execute this code:
 * `
    $product = new WC_Product( $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' );
 * It works:
 * `
    Array ( [1078] => Array ( [title] => H1499T-1495P [description] => LUMINAIRE
   ENCASTRÉ TYPE GIMBAL MR16 DE COULEUR BLANC [Lien_Photo] => HALO_1495.jpg [Lien_Specsheet]
   => HALO 1495.pdf [Photometrie] => Non Disponible [ID_Famille] => Luminaires Downlight[
   ID_Sous_Famille] => Orientable [ID_Manufacturier] => Eaton [ID_Marque] => Halo[
   ID_Serie] => HALO 1495 [ID_Voltage] => 120V [ID_Source] => INC [ID_Type_de_source]
   => MR16 [ID_Installation] => Encastré Rond 4'' [guide_stock] => O [Prix_Budget]
   => $65 [Lumen_emis] => ? [Wattage] => ? [id_es] => N [id_dlc] => N [id_dlcp] 
   => N ) )
 * When I try this code:
 * `
    $product->get_attributes();
 * It doesn’t work, all values are empty:
 * `
    Array ( [pa_id_gs] => Array ( [name] => pa_id_gs [value] => [position] => 
   0 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_gd] => Array ( [name] => pa_id_gd [value] => [position] => 1 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_id_t20] => Array ( [name] => pa_id_t20 [value] => [position] => 2 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_id_es] => Array ( [name] => pa_id_es [value] => [position] => 3 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_id_dlc] => Array ( [name] => pa_id_dlc [value] => [position] => 4 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_id_dlcp] => Array ( [name] => pa_id_dlcp [value] => [position] => 5 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_numero_de_catalogue] => Array ( [name] => pa_numero_de_catalogue [value] =
   > [position] => 6 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_voltage] => Array ( [name] => pa_id_voltage [value] => [position]
   => 7 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_manufacturier] => Array ( [name] => pa_id_manufacturier [value]
   => [position] => 8 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [
   is_create_taxonomy_terms] => 1 ) [pa_id_marque] => Array ( [name] => pa_id_marque[
   value] => [position] => 9 [is_visible] => 1 [is_variation] => 1 [is_taxonomy]
   => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_serie] => Array ( [name] => pa_id_serie[
   value] => [position] => 10 [is_visible] => 1 [is_variation] => 1 [is_taxonomy]
   => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_famille] => Array ( [name] => pa_id_famille[
   value] => [position] => 11 [is_visible] => 1 [is_variation] => 1 [is_taxonomy]
   => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_sous_famille] => Array ( [name]
   => pa_id_sous_famille [value] => [position] => 12 [is_visible] => 1 [is_variation]
   => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_id_installation]
   => Array ( [name] => pa_id_installation [value] => [position] => 13 [is_visible]
   => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 )[
   pa_id_source] => Array ( [name] => pa_id_source [value] => [position] => 14 [
   is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_type_de_source] => Array ( [name] => pa_id_type_de_source [value]
   => [position] => 15 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [
   is_create_taxonomy_terms] => 1 ) [pa_lumen_emis] => Array ( [name] => pa_lumen_emis[
   value] => [position] => 16 [is_visible] => 1 [is_variation] => 1 [is_taxonomy]
   => 1 [is_create_taxonomy_terms] => 1 ) [pa_wattage] => Array ( [name] => pa_wattage[
   value] => [position] => 17 [is_visible] => 1 [is_variation] => 1 [is_taxonomy]
   => 1 [is_create_taxonomy_terms] => 1 ) [pa_description] => Array ( [name] => 
   pa_description [value] => [position] => 18 [is_visible] => 1 [is_variation] =
   > 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_prix_budget] => 
   Array ( [name] => pa_prix_budget [value] => [position] => 19 [is_visible] => 
   1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [pa_lien_photo]
   => Array ( [name] => pa_lien_photo [value] => [position] => 20 [is_visible] =
   > 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms] => 1 ) [
   pa_lien_specsheet] => Array ( [name] => pa_lien_specsheet [value] => [position]
   => 21 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_photometrie] => Array ( [name] => pa_photometrie [value] => [position]
   => 22 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_produit] => Array ( [name] => pa_id_produit [value] => [position]
   => 23 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) [pa_id_actif] => Array ( [name] => pa_id_actif [value] => [position] =
   > 24 [is_visible] => 1 [is_variation] => 1 [is_taxonomy] => 1 [is_create_taxonomy_terms]
   => 1 ) )
 * Is it because it’s taxonomy terms ?
 * Like I said in my previous message, I tried a lot of things, nothings work.
 * Why can’t I retrieve all the attributes in one call ?
 * Thanks!
 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656872)
 * > I don’t think I can use a widget to do what I need.
 * I’m asking why that is the case. Have you tried using the widgets? It sounds 
   an awful lot like you are trying to rebuild their exact functionality.
 * When I run `get_attributes()`, some values are empty – but variable products 
   do show values.
 * Could you explain maybe what you’re end result is though, and why the default
   layered nav widgets won’t work?
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656875)
 * See: [http://meprogweb.com/temp/Clipboard01.png](http://meprogweb.com/temp/Clipboard01.png)
 * When a checkbox is checked, all the other data from this category needs to hide,
   and the amount of products by data needs to update. I’m doing it in jQuery.
 * Which widget can I use to do that?
 * So, why can’t I get my attributes with the get_attributes() function?
 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656900)
 * Yea, that is how the layered nav widgets work: [https://docs.woocommerce.com/document/woocommerce-widgets/#section-5](https://docs.woocommerce.com/document/woocommerce-widgets/#section-5).
   Just need to use global attributes.
 * > So, why can’t I get my attributes with the get_attributes() function?
 * Not sure.
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656906)
 * In the link you sent me, it says:
    The layered navigation widget only appears
   on product archives.
 * Can I show the filters with shorttag or something else ?
 * My code is all done, I just need to speed up the thing. I’d really like to understand
   why I can’t retrieve all the attributes with one call (get_attributes)…
 *  [Danny Santoro](https://wordpress.org/support/users/danielsantoro/)
 * (@danielsantoro)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656909)
 * The filters are shown through a sidebar widget – there are actually a few included
   in Ajax Layered Navigation, like the filter itself (where you can choose if/and
   etc), a list of active tags someone could click off to remove, and a clear all
   button.
 *  [Caleb Burks](https://wordpress.org/support/users/icaleb/)
 * (@icaleb)
 * Automattic Happiness Engineer
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656910)
 * > Can I show the filters with shorttag or something else ?
 * Nope, it has to be an archive page to work. Why not use the archives created?
   You can use tags or categories to make specific pages.
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656912)
 * It needs to be in the account of the connected user. Because the user can create
   a project, and put products in it. And the filter needs to be when the user is
   making his product list for a particular project. All that is already coded. 
   I am a PHP programmer, so I don’t like to be in WordPress when I want to code
   something custom. 😉 My problem is resumed here: [https://wordpress.org/support/topic/get_attributes-problem-when-get_attribute-works-fine](https://wordpress.org/support/topic/get_attributes-problem-when-get_attribute-works-fine)
 * Thanks for your help!!
 *  Thread Starter [MarieveC](https://wordpress.org/support/users/marievec/)
 * (@marievec)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656940)
 * Problem solved here:
    [https://wordpress.org/support/topic/get_attributes-problem-when-get_attribute-works-fine](https://wordpress.org/support/topic/get_attributes-problem-when-get_attribute-works-fine)

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Speed problem on loading, PHP code with multiple queries in loop’ is 
closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [attributes](https://wordpress.org/support/topic-tag/attributes/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [products](https://wordpress.org/support/topic-tag/products/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [speed](https://wordpress.org/support/topic-tag/speed/)

 * 15 replies
 * 4 participants
 * Last reply from: [MarieveC](https://wordpress.org/support/users/marievec/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/speed-problem-on-loading-php-code-with-multiple-queries-in-loop-1/#post-7656940)
 * Status: resolved