frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

WooCommerce - How to get featured products with WP_Query

I would like to display 6 featured products from my woocommerce store on my home-page.php template. After some researched I found that the right way to do this was through a custom loop,( I do not wish to use shortcodes because I would like to add additional classes for styling etc. ) I also found that the key that woocommerce uses for the featured products is '_featured'. I put together the code below to display any products that I chose to be featured products in my store, but it doesn't work... Any help is appreciated.

<?php

    $args = array(
        'post_type'   => 'product',
        'stock'       => 1,
        'showposts'   => 6,
        'orderby'     => 'date',
        'order'       => 'DESC' ,
        'meta_query'  => array(
            array(
                'key'     => '_featured',
                'value'   => 0,
                'compare' => '>',
                'type'    => 'numeric'
            )
        )
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

        <li>    
            <?php 
                if ( has_post_thumbnail( $loop->post->ID ) ) 
                    echo get_the_post_thumbnail( $loop->post->ID, 'shop_catalog' ); 
                else 
                    echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="65px" height="115px" />'; 
            ?>
            <h3><?php the_title(); ?></h3>

            <?php 
                echo $product->get_price_html(); 
                woocommerce_template_loop_add_to_cart( $loop->post, $product );
            ?>    
        </li>

<?php 
    endwhile;
    wp_reset_query(); 
?>

Comments

  • There are many answers here but the correct answer is

    This has changed in WooCommerce 3.0. It's not simply a meta_query, but now includes a tax_query. The arguments are now:

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

    See woocommerce/includes/class-wc-shortcodes.php

    or If you are completely doing the custom query

        $query_args = array('post_type' => 'product' , 'showposts' => $num , 'order_by' => $sort , 'order' => $order);
    
        if( $cat ) $query_args['product_cat'] = $cat;
    
        $query_args['tax_query'] = array( array('taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => array('featured')) );
        $query = new WP_Query($query_args) ;
    
Sign In or Register to comment.

The Forum Hub

| make vanilla clean and light
@ 2018 The Forum Hub, All rights reserved.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

admin@theforumhub.com
support@theforumhub.com
(800) 3032120

Get In Touch