By MMT


2019-05-14 10:34:34 8 Comments


I am trying to list my products using WP_Query. I have used a query such as the following one:

$slider_products_q = new WP_Query([
     'posts_per_page'    => 8,
      'post__in'          => $slides_product_ids,
      'post_type'         => 'product',
      'orderby'           => 'date',
      'order'             => 'DESC'
]);

It works perfectly, however when I want to access WooCommerce methods inside the loop such as get_html_price it becomes a bit tricky.

I have managed to handled it using this way, yet the performace is not good and the query takes about '0.7' seconds to load up.

This is how I loop though each item:

if ($slider_products_q->have_posts()):
   while($slider_products_q->have_posts()):
       $slider_products_q->the_post();         
       $product_id = get_the_ID();
       global $product;
       ...

Then I could call $product->get_price_html(); method.

I also did try to query products using Woocommerce methods but since they load up a lot of custom attributes the query takes even much more time.

Any suggestion of how to improve this?

By the way I only care about using Woocommerce methods such as get_price_html.
Attributes or custom terms are not necessary at all.

1 comments

@LoicTheAztec 2019-05-14 16:57:18

The global $product; doesn't work in a custom WP_Query, instead use wc_get_product() like:

if ($slider_products_q->have_posts()):
   while($slider_products_q->have_posts()):
       $slider_products_q->the_post();         
       $product_id = get_the_ID();
       $product = wc_get_product($product_id);

       $price_html = $product->get_price_html()
       // ...

Now you can use any WC_Product methods on $product variable.

Alternatively, you could use a WC_Product_query.

@MMT 2019-05-15 07:13:08

If I use wc_get_product it will be disastrous, since another query is going to take place beside the WP_Query. I will try using WC_Product_query hope it does help, but if this method loads attributes while getting products, another massive query will happen considering my website have more that 20000 attributes in woocommerce.

@LoicTheAztec 2019-05-15 07:49:21

@MMT So use directly a WC_Product_Query instead. There is no other ways if you want to use methods as get_price_html()…

@MMT 2019-05-15 07:55:36

I will try it and inform you of the results.

@MMT 2019-05-15 12:49:15

I had tested your solution but mine was much faster

@LoicTheAztec 2019-05-15 16:07:18

@MMT What solution? Your question is not about a faster solution, but about how to use WC_Product methods on a WP_Query and in your code how to get the WC_Product Object… My answer answers that.

@MMT 2019-05-17 06:45:34

unfortunately @fuxia changed my original question

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Woocommerce pagination and result count after custom loop

1 Answered Questions

How do I update product shipping using PHP in WooCommerce?

  • 2015-12-09 20:47:47
  • Krista Lynn Kelly
  • 3987 View
  • -1 Score
  • 1 Answer
  • Tags:   php woocommerce

1 Answered Questions

0 Answered Questions

Alternative or workaround for wp_insert_attachment

1 Answered Questions

1 Answered Questions

WooCommerce is slow with many categories

  • 2014-02-04 22:54:05
  • Kieran 'Kizbo' Barnes
  • 4925 View
  • 3 Score
  • 1 Answer
  • Tags:   woocommerce

1 Answered Questions

[SOLVED] Pagination works in custom query loop but it doesn't work in shortcode

  • 2013-10-31 03:52:45
  • elenakoroleva
  • 1079 View
  • 0 Score
  • 1 Answer
  • Tags:   wp-query pagination

1 Answered Questions

1 Answered Questions

[SOLVED] Sub-loop / nested loops Best Practices

Sponsored Content