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

wp query - show most viewed post

programmeradmin2浏览0评论

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'

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论