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

meta query - Display no post when metavalue is 0

programmeradmin4浏览0评论

There is a page displaying the 3 most popular posts, using the post views on the query. When there are no post views, I don't want to display any posts.

How can I display no post when post_views_count is 0? I think I'm missing something in my meta query. What's wrong?

$query_args = array(
    'post_type' => 'list',
    'posts_per_page' => 3,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query' => array(
    array(
    'meta_key' => 'post_views_count',
    'value' => 50,
        'compare' => '>'
        )
    ),
);


                                    

There is a page displaying the 3 most popular posts, using the post views on the query. When there are no post views, I don't want to display any posts.

How can I display no post when post_views_count is 0? I think I'm missing something in my meta query. What's wrong?

$query_args = array(
    'post_type' => 'list',
    'posts_per_page' => 3,
    'meta_key' => 'post_views_count',
    'orderby' => 'meta_value_num',
    'order' => 'DESC',
    'meta_query' => array(
    array(
    'meta_key' => 'post_views_count',
    'value' => 50,
        'compare' => '>'
        )
    ),
);


                                    
Share Improve this question edited Jan 28, 2022 at 20:26 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Jan 28, 2022 at 5:39 Fernando SouzaFernando Souza 1512 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Here's an updated example that appears to do what you want. See the docs for meta queries for details.

$query_args = [
    'post_type'      => 'list',
    'posts_per_page' => 3,
    'orderby'        => 'meta_value_num',
    'order'          => 'DESC',
    'meta_query'     => [
        [
            'key'      => 'post_views_count',
            'value'    => 50,
            'compare'  => '>',
            'type'     => 'NUMERIC',
        ],
    ],
];

$post_views_query = new WP_Query( $query_args );
发布评论

评论列表(0)

  1. 暂无评论