Populate the side area with widgets, images, navigation links and whatever else comes to your mind.
18 Northumberland Avenue, London, UK
(+44) 871.075.0336
Follow us

WooCommerce: Show Products With Specific Minimum Quantity

WooCommerce: Show Products With Specific Minimum Quantity

I had a task last week where I needed to display products having a specific minimum quantity in stock. I looked around but couldn’t find a solution. However, after some digging around I came across a solution in the form of meta_query.

The meta_query class handles meta-specific queries and generates the necessary SQL to do the query. So in order for me to filter products by stock quantity, I’d have to add meta query to the main WordPress loop that is responsible for outputting the products in the shop page. The meta associated with stock quantity is _stock.

Here’s the code that you can use. This implementation is by no means complete. Use the appropriate method to add the code to your pages making sure that you follow WordPress best practices.

global $wp_query;
$meta_query = array('meta_query' => array(
                      'key' => '_stock',
                      'value' => '5',
                      'compare' => '>=',
			'type' => 'NUMERIC'
$args = array_merge( $wp_query->query_vars, $meta_query );
query_posts( $args );

A couple of things to note:

  1. compare key: this allows you to specify a way to compare the meta value. In my case I wanted to only show products with a quantity of 5, so I used ‘>=’,
  2. type key: this is important for the comparison to work well. Depending on the type you want to compare, use the appropriate compare tag

That’s all there is to it. If you have any comments/suggestions please share them in the comments section.

Comment: 1

  • astor
    June 30, 2015 10:31 am

    Hi, work for variable product?
    I wrote a similar shortcode…but if I have variations on products and set stock status for each variations…don’t work…
    my query is like this
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    ‘key’ => ‘_stock_status’,
    ‘value’ => ‘instock’,
    ‘compare’ => ‘IN’
    ‘key’ => ‘_stock’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘>=’,
    ‘type’ => ‘NUMERIC’


    can you help me?

Sorry, the comment form is closed at this time.