wp query - show most viewed post

I have blog post counts which happen when a user views a blog post via ajax and the rest api.In the database I have a m

I have blog post counts which happen when a user views a blog post via ajax and the rest api.

In the database I have a meta key and value like so:

table: wp_postmeta
post_id: blog post ID
meta_key: blog_post_count
meta_value: number ie: 1, 2, 3 etc. 

This basically stores the blog post ID and the count. On a page, I am trying to display the ID with the highest count only.

$views = new WP_Query( array(
    'orderby' => 'meta_value_num',
    'meta_key' => 'blog_post_count',
    'order' => 'DESC',
    'posts_per_page' => 1
) );

Running this in a standard wordpress loop however displays all posts and not with the highest count first so my Query is obviously not correct.

I have blog post counts which happen when a user views a blog post via ajax and the rest api.

In the database I have a meta key and value like so:

table: wp_postmeta
post_id: blog post ID
meta_key: blog_post_count
meta_value: number ie: 1, 2, 3 etc. 

This basically stores the blog post ID and the count. On a page, I am trying to display the ID with the highest count only.

$views = new WP_Query( array(
    'orderby' => 'meta_value_num',
    'meta_key' => 'blog_post_count',
    'order' => 'DESC',
    'posts_per_page' => 1
) );

Running this in a standard wordpress loop however displays all posts and not with the highest count first so my Query is obviously not correct.

Share Improve this question edited Jun 24, 2020 at 9:34 user8463989 asked Jun 24, 2020 at 9:17 user8463989user8463989 5931 gold badge8 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
try changing 'orderby' => 'meta_value_num'  to 'orderby' => 'meta_value meta_value_num'

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742322521a4422099.html

相关推荐

  • wp query - show most viewed post

    I have blog post counts which happen when a user views a blog post via ajax and the rest api.In the database I have a m

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信