$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WP Query - Get WooCommerce Products with variation that is in stock|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WP Query - Get WooCommerce Products with variation that is in stock

matteradmin9PV0评论

I am creating a clothing store. The clothing are variable products based on size (xs, s, m, l, xl etc..). I am trying to create filters to display products that are in stock, based on a filter. So, clicking the "xs" filter should show products that both have the XS variation, and have that variation in stock.

So far, I've got something like this:

$query_args = array(
 'post_type' => 'product', 'product_variation',
 'posts_per_page' => 12,
 'paged' => $paged,
 'tax_query' => array(
    array(
    'taxonomy' => 'pa_sizes',
    'field' => 'term_id',
    'terms' => 'xs',
    ),
 ),
 'meta_query' => array(
    array(
    'key' => '_stock_status',
    'value' => 'outofstock',
    'compare' => '='
    ),
 )
);

$query = new WP_Query($query_args);

I know right now it's searching for "outofstock" but that was just a test, and not even this comes up with any products, even though I have created some products with some sizes out of stock.

I am creating a clothing store. The clothing are variable products based on size (xs, s, m, l, xl etc..). I am trying to create filters to display products that are in stock, based on a filter. So, clicking the "xs" filter should show products that both have the XS variation, and have that variation in stock.

So far, I've got something like this:

$query_args = array(
 'post_type' => 'product', 'product_variation',
 'posts_per_page' => 12,
 'paged' => $paged,
 'tax_query' => array(
    array(
    'taxonomy' => 'pa_sizes',
    'field' => 'term_id',
    'terms' => 'xs',
    ),
 ),
 'meta_query' => array(
    array(
    'key' => '_stock_status',
    'value' => 'outofstock',
    'compare' => '='
    ),
 )
);

$query = new WP_Query($query_args);

I know right now it's searching for "outofstock" but that was just a test, and not even this comes up with any products, even though I have created some products with some sizes out of stock.

Share Improve this question asked Mar 17, 2018 at 1:37 Jordan CarterJordan Carter 2912 gold badges5 silver badges13 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You will have to change your tax_query. The xs is not the term_id but the slug

So your tax_query will end up being like

'tax_query' => array(
    array(
       'taxonomy' => 'pa_sizes',
       'field' => 'slug',
       'terms' => 'xs'
    )
 )

I would love to help with the "instock" problem too, but I'm stuck with that question too.

Isn't this way correct?

'post_type' => array('product', 'product_variation'),

Your $query_args is broken array

Post a comment

comment list (0)

  1. No comments so far