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 badges1 Answer
Reset to default 0try changing 'orderby' => 'meta_value_num' to 'orderby' => 'meta_value meta_value_num'