2

I was wondering if someone could help me. I am trying to display featured products using a custom loop. Before WooCommerce 3 I used following loop, which worked perfectly:

<?php

$args = [  
    'post_type' => 'product',  
    'meta_key' => '_featured',  
    'meta_value' => 'yes',  
    'posts_per_page' => 6  
];  

$featured_query = new WP_Query( $args );  

if ($featured_query->have_posts()) :   

    while ($featured_query->have_posts()) :   

        $featured_query->the_post();  

        $product = get_product( $featured_query->post->ID );  

        // here is my output 

    endwhile;  

endif;  

wp_reset_query();

?>

Since the update to WooCommerce 3, the meta_key _featured does not exist anymore. I figured out that featured products are using the new product_visibility taxonomy instead of meta now. Unfortunately I do not know how to change my loop to output the featured products now. I want to use custom styles, so I do not want to use a shortcode. Can anyone help me?

1
  • WP_Query() and get_posts() are both not recommended ways of doing this. I wrote an detailed example of how to do this the correct way: cfxdesign.com/…
    – cfx
    Commented May 24, 2018 at 19:00

1 Answer 1

8

I got the same problem. Try this ! Works for me

<?php
     $featured_query = new WP_Query( array(
         'tax_query' => array(
                 array(
                     'taxonomy' => 'product_visibility',
                     'field'    => 'name',
                     'terms'    => 'featured',
                     'operator' => 'IN'
                 ),
          ),
     ) );
?>
4
  • Thanks for the answer. Please explain how this solve the problem.
    – Laxmana
    Commented Apr 25, 2017 at 11:33
  • Thank you so much, it works! I missed the fact that the tax_query is an array in an array! Thanks, mate!
    – Christoph
    Commented Apr 25, 2017 at 22:50
  • @Laxmana Woocommerce recently changed the featured flag from a meta entry to a term, which is why running a tax_query for that term works.
    – jammypeach
    Commented Jun 13, 2017 at 8:50
  • I searched for this information for a long time. Thank you.
    – GRY
    Commented Dec 6, 2017 at 0:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.