How to fetch WooCommerce Featured Products using WP_Query

Tara Prasad Swain MySql, PHP, WordPress

In this article I am going to explain how to fetch WooCommerce featured products in WordPres using WP_QUERY.

To fetch featured products please check below code.


$meta_query  = WC()->query->get_meta_query();
    $tax_query   = WC()->query->get_tax_query();
    $tax_query[] = array(
        'taxonomy' => 'product_visibility',
        'field'    => 'name',
        'terms'    => 'featured',
        'operator' => 'IN',
    );

    $args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => $atts['per_page'],
        'orderby'             => $atts['orderby'],
        'order'               => $atts['order'],
        'meta_query'          => $meta_query,
        'tax_query'           => $tax_query,
    );

$the_query = new WP_Query( $args );

Product_visibility is a custom taxonomy is used in tax query.

Hope this helps.

Share this Post