admin管理员组文章数量:1278910
Here is how I am getting the views for one post:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
Let's say I want to find the most viewed posts from 5,000 posts and I want to show the top 5 most viewed posts.
How can I make a query to achieve this?
Here is how I am getting the views for one post:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
Let's say I want to find the most viewed posts from 5,000 posts and I want to show the top 5 most viewed posts.
How can I make a query to achieve this?
Share Improve this question asked Apr 17, 2012 at 18:54 RomesRomes 4554 gold badges11 silver badges20 bronze badges2 Answers
Reset to default 18View this section of the Codex to learn how to create a custom query: http://codex.wordpress/Class_Reference/WP_Query
Your query will be something like:
$query = new WP_Query( array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'posts_per_page' => 5
) );
By default, the ordering will be highest to lowest, thus giving you the "top" 5.
View this section of the Codex to learn how to create a custom query: http://codex.wordpress/Class_Reference/WP_Query
this code will work
$query = new WP_Query( array(
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'posts_per_page' => 5
) );
本文标签: How to query for most viewed posts and show top 5
版权声明:本文标题:How to query for most viewed posts and show top 5 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741242964a2364338.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论