Darius
Darius

Reputation: 1664

Limit Woocommerce featured products in a WP_Query

I want to get 3 featured products in the header of the site. But my query keeps returning unlimited number of results.

I've been looking online for a solution and came across answers that all answer saying the same thing in terms of the query. What could I be doing wrong?

$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',
    'posts_per_page'      => 2,
    'meta_query'          => $meta_query,
    'tax_query'           => $tax_query,
);

$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 );

        echo $product->title; echo "test";
        // Product info here

    endwhile;

}

wp_reset_query();

The following query returned 20 results. The code was placed in header.php. Using woocommerce 3.x.

Upvotes: 1

Views: 1870

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253804

First your code is a bit outdated, since Woocommerce 3, as get_product() need to be replaced with wc_get_product() and $product->title; by $product->get_title();
Once done your code works and you will get 3 featured products:

$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',
);

$featured = new WP_Query( array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'posts_per_page'      => 3, // <==  <==  <==  3 products
    'meta_query'          => $meta_query,
    'tax_query'           => $tax_query,
) );

// Get the products count in the query
echo '<p>Featured products count: ' .$featured->post_count . '</p>';

if ($featured->have_posts()) : while ($featured->have_posts()) : 
    $featured->the_post();

    $product = wc_get_product( $featured->post->ID );

    echo $product->get_title() . '<br>';
    // Product info here

endwhile; endif;

wp_reset_postdata();

It should work for you as I have tested successfully this code on header.php file…

As before Woocommerce 3, the "featured products" where handled by post meta data (a meta query), you may need to update product terms count going to Woocommerce settings > status > tools. In "Term counts" section click on "Recount terms".

Upvotes: 2

Xhynk
Xhynk

Reputation: 13840

You should be using wp_reset_postdata() instead of wp_reset_query() since WP_query doesn't overwrite the main query.

If that doesn't solve your issue, make sure any other custom loops use the appropriate reset, and/or try renaming the variable $featured_query if you're using it elsewhere - it may be inheriting posts from a previous loop.

You could also try adding the 'nopaging' => true and 'ignore_sticky_posts' => true arguments

I hate to suggest it, but if you can't figure out why it's returning 20 posts instead of 2, you could just break your while loop with a counter:

if ($featured_query->have_posts()) {
    $counter = 0;
    while ($featured_query->have_posts()) : $featured_query->the_post();

        /* Do post stuff here */

        $counter++;

        if( $counter == 2 ) break;
    endwhile;
}

Upvotes: 2

Related Questions