I have manually edited a WordPress WooCommerce hook which hides all out of stock products. The problem is I don't know how to count items found.
I tried $query->count
but no result.
My code:
function hide_products($query) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key' => '_stock_status',
'compare' => 'NOT IN',
'value' => 'outofstock'
);
$query->set( 'meta_query', $meta_query );
}
I have manually edited a WordPress WooCommerce hook which hides all out of stock products. The problem is I don't know how to count items found.
I tried $query->count
but no result.
My code:
function hide_products($query) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key' => '_stock_status',
'compare' => 'NOT IN',
'value' => 'outofstock'
);
$query->set( 'meta_query', $meta_query );
}
Share
Improve this question
asked Sep 26, 2019 at 12:14
Ru LevaRu Leva
11 bronze badge
1 Answer
Reset to default 0$query->found_posts may be what you are looking for from the code reference:
https://developer.wordpress/reference/classes/wp_query/
Note that meta_query expects nested arrays, even if you only have one query. You may need to use:
$meta_query[0][] = array( .....
You should also take care with the form of the original query. If there are one or more relations defined, you will need to insert your new conditions into the correct relation array.