16

I updated WooCommerce to version 3.0 but I can't show the featured products on my theme, I googled a while and get WC deleted the _feature and add this in taxonomy. But I don't understand so much how my theme get the featured products.

Here is the code of the wrong featured productcs.

$meta_query   = WC()->query->get_meta_query();
    $meta_query[] = array(
        'key'   => '_featured',
        'value' => 'yes'
    );

    $args = array(
        'post_type'           => 'product',
        'post_status'         => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page'      => $products,
        'orderby'             => $orderby,
        'order'               => $order == 'asc' ? 'asc' : 'desc',
        'meta_query'          => $meta_query
    );

And if you know where is the featured item in the DataBase. Thanks so much.

3
  • How are you trying to display the featured products? Are you trying to run a WP_Query? I can't tell what is happening in your code. Have you considered using the [featured_products] shortcode? Sep 15, 2017 at 19:58
  • How I can use [featured_products] shortcode? Sep 15, 2017 at 20:15
  • The theme itself made the shortcodes for the theme and these shortcode are the problem. This extract are part of the code. Sep 15, 2017 at 20:16

4 Answers 4

41

Since Woocommerce 3, you have to use a Tax Query instead as featured products are now handled by product_visibility custom taxonomy for the term featured:

// The tax query
$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN', // or 'NOT IN' to exclude feature products
);

// The query
$query = new WP_Query( array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'tax_query'           => $tax_query // <===
) );

References:

You could use wc_get_featured_product_ids() function to get the featured product IDs array but using a tax query in a WP_Query is just fine and the right way…

Related:

It should work.

0
15

This is an old question, but you can use wc_get_featured_product_ids() too:

$args = array(
    'post_type'           => 'product',
    'posts_per_page'      => $products,
    'orderby'             => $orderby,
    'order'               => $order == 'asc' ? 'asc' : 'desc',
    'post__in'            => wc_get_featured_product_ids(),
);

$query = new WP_Query( $args );

Just discovered it here. I hope it helps!

1
  • Thanks, it works great! I don't understand why they moved from a postmeta to a taxonomy, makes no sense.
    – Caio Mar
    Feb 12, 2019 at 14:42
11

You can now use wc_get_products with parameter featured set to true. See https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

$args = array(
    'featured' => true,
);
$products = wc_get_products( $args );

For people looking for getting featured products by category then you can check my notes about this => https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/

5
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 12,
        'tax_query' => array(
                array(
                    'taxonomy' => 'product_visibility',
                    'field'    => 'name',
                    'terms'    => 'featured',
                ),
            ),
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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