List Woocommerce Featured Product without Shortcode

Okay so i was working on a project and had to list Woo-commerce featured products so i come up with this solution


$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'stock' => 1,
'showposts' => 6,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query
);
$featured_query = new WP_Query( $args );

But it did not work completely fine for me and come up with this solution then,


$featured_query = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
) );

So if first does not work, try second one.

Leave a Comment