WooCommerce vs WP_Query: Get sale products

By 0 Comments

Sometime, to sell more and more products, a custom module with list of sale off products is very important.

This module help your customer don’t miss a gift from you.

You can find and use some free plugin on wordpress.org
And, why do you don’t create a custom module for yourself.

Let’s do it with WP_Query class


$posts_per_page = 10;

$meta_query = WC()->query->get_meta_query();

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

$product_ids_on_sale = wc_get_product_ids_on_sale();

$args = array(
	'posts_per_page'	=> $posts_per_page,
	'orderby' 		=> $atts['orderby'],
	'order' 		=> $atts['order'],
	'no_found_rows' 	=> 1,
	'post_status' 		=> 'publish',
	'post_type' 		=> 'product',
	'meta_query' 		=> $meta_query,
	'post__in'		=> array_merge( array( 0 ), $product_ids_on_sale )

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