最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to count items of meta query?

programmeradmin0浏览0评论

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
Add a comment  | 

1 Answer 1

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论