admin管理员组

文章数量:1427301


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.


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.

Share Improve this question edited May 14, 2019 at 19:26 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 14, 2019 at 10:34 MeMoMeMo 812 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: wp queryHow to get the WCProduct Object when using a WPQuery in WooCommerce