WooCommerce

WooCommerce vs WP_Query: Get featured products

By 2 Comments

WooCommerce comes with a number of useful shortcodes to allow quick insertion of WooCommerce related content within posts and pages. One of these shortcodes can be used to display featured products like so:

[featured_products per_page="12" columns="4"]

So wonderful! However, what if we want go one step further and have more control over the products shown, or if we’re already using the shortcode elsewhere on our site?

You can build a custom widget, a custom shortcode or a custom template, and display “featured products” from WooCommerce. Let’s using WP_Query class like so:

$posts_per_page = 10;

$atts = array(
	'orderby' => 'title',
	'order'   => 'asc');

$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'      => $posts_per_page,
	'orderby'             => $atts['orderby'],
	'order'               => $atts['order'],
	'meta_query'          => $meta_query
);

$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );

 

How to make a product to featured ?

On Admin Dashboard, go to Products, on product data grid, find and click star icon.

make a product to featured