我正在我的网站上实现一个部分,以获得使用此代码的前5位作者。
function ranked_authors(){
$usrs = get_users(\'role=contributor\');
$countarr = array();
foreach ($usrs as $usr) {
$post_views = total_no_of_post_views($usr->ID);
$countarr[$usr->ID] = $post_views;
}
arsort($countarr);
$i=1;
foreach($countarr as $id => $pcount){
if($i<=5){
$div_media = \'<div class="media clearfix">
<div class="media-left">\';
$author_link = get_author_posts_url($id);
$author_name = get_the_author_meta(\'display_name\',$id);
$avatar = get_avatar_url( $id, \'size=50\' );
$div_media .= \'<a href="\'.$author_link.\'">\'.get_avatar($id, \'size=50\').\'
<span class="rank">\'.$i.\'</span>
</a>
</div>
<div class="media-body">\';
$div_media .= \'<h4 class="media-heading"><a href="\'.$author_link.\'">\'.$author_name.\'</a></h4>\'.$pcount.\' Total Views</div>
</div>\';
echo $div_media;
$i++;
}
}
}
我的博客上有4000多篇帖子,所以这个查询需要花费大量时间来执行,从服务器处理和返回数据几乎需要10秒钟。然后我想用ajax的方式来做。但我的代码中有一个问题,当我
returning(return)
结果是如果我
echo
这个函数然后通过ajax请求返回数据。我不知道,但我做错了。
我想根据帖子总浏览量对作者进行排序。这是我用来计算帖子浏览总数的函数。
function total_no_of_post_views($author_id){
$sumViews = 0;
$args = array(
\'author\' => $author_id,
\'post_type\' => array(\'communityposts\',\'post\',\'video\'),
\'posts_per_page\'=> -1
);
$author_query = new WP_Query( $args );
if( $author_query->have_posts() ) : while( $author_query->have_posts() ) : $author_query->the_post();
$sumViews += get_post_meta( get_the_ID(), \'cv_post_views_count\', true );
endwhile; endif;
wp_reset_postdata();
return $sumViews;
}
我的最终要求是根据发表文章的总浏览次数和发表文章的价值来计算排名前5位的作者。
最合适的回答,由SO网友:TheDeadMedic 整理而成
您需要一个自定义查询-这将把POST、POSTETA和usermeta表连接在一起,然后计算所有communityposts
, post
和video
按作者分组的视图:
function wpse_190303_get_popular_users( $number = 5 ) {
if ( ! $number = absint( $number ) )
return;
global $wpdb;
$query = "
SELECT
SUM( $wpdb->postmeta.meta_value+0 ) AS `post_views`,
$wpdb->usermeta.user_id AS `user_id`
FROM
$wpdb->posts
INNER JOIN
$wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id
INNER JOIN
$wpdb->usermeta ON $wpdb->posts.post_author = $wpdb->usermeta.user_id
WHERE
$wpdb->posts.post_type IN( \'communityposts\', \'post\', \'video\' ) AND
$wpdb->posts.post_status = \'publish\' AND
$wpdb->postmeta.meta_key = \'cv_post_views_count\' AND
$wpdb->usermeta.meta_key = \'{$wpdb->prefix}capabilities\' AND
$wpdb->usermeta.meta_value LIKE \'%\\\\\\"contributor\\\\\\"%\'
GROUP BY
$wpdb->usermeta.user_id
ORDER BY
post_views DESC
LIMIT
$number
";
if ( $result = $wpdb->get_results( $query ) )
cache_users( wp_list_pluck( $result, \'user_id\' ) );
return $result;
}
要使用:
if ( $data = wpse_190303_get_popular_users() ) {
foreach ( $data as $item ) {
$user_id = $item->user_id;
$views = $item->post_views;
// Your code to output the popular authors
}
}
SO网友:TheDeadMedic
我猜你的工作效率极低total_no_of_post_views
仅为一个用户在posts表上运行计数查询?
相反,请使用get_users
要在查询过程中获得排名前5位的作者(而不是遍历所有作者并统计每个用户的帖子),请执行以下操作:
get_users(
array(
\'role\' => \'contributor\',
\'number\' => 5, // Number of users to retrieve
\'count_total\' => false, // Don\'t SQL_CALC_FOUND_ROWS
\'meta_key\' => \'cv_post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
)
);