Woocommerce Product Request for Stock Status

I use 3 availability statuses: “in stock”, “out of stock” and “allow backups”. I want to export products that are only in stock status for XML. The problem is that woocommerce returns an “instock” value for both statuses: “in stock” and “allow backups”. Now the request looks like this:

$query = array( 'post_type' => 'product', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock' ) ) ); $wp_query = & new WP_Query($query); while ($wp_query->have_posts()) : $wp_query->the_post(); 

And it exports products with instock and backorders_allowed statuses. Perhaps there is a way to exclude products with "backorders_allowed".

+5
source share
1 answer

You may have several meta_query filters. See ( http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters ). By default, the relationship between these filters is AND, and this is normal. You can add a filter for _back_order = no .

 $query = array( 'post_type' => 'product', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock' ), array( 'key' => '_backorders', 'value' => 'no' ), ) ); $wp_query = & new WP_Query($query); while ($wp_query->have_posts()) : $wp_query->the_post(); 
+4
source

Source: https://habr.com/ru/post/1245716/


All Articles