我在WordPress中找到了下面的代码来统计帖子浏览量。我需要这两件事,但我还不能做到。
作者统计:如何通过快捷码显示当前登录用户、所有已发布帖子的总视图。我该怎么办?我在WPSE上找到了一个显示总视图的答案,但它没有添加短代码并获取所有帖子的视图,我想要所有已发布帖子的视图
为了表示数据,如何在html表中显示每个作者的视图,而不仅仅是当前作者、所有作者,只显示作者姓名和帖子总视图。这对于多作者站点很有用,并可用于跟踪所有作者的性能。
// function to display number of posts.
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\';
}
// function to count views.
function setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
我试图对当前用户的所有帖子ID进行数组,但失败了。我还尝试在这篇文章中添加一个作者id部分,以使其更简单。但似乎没有一个奏效。
下面是当前的代码实现
function getPostViews($postID){
$count_key = \'Creation_Views\';
$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\';
}
function setPostViews($postID) {
$count_key = \'Creation_Views\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
function get_meta_value_by_meta_key($key, $values, $sum){
global $wpdb;
$metaValues = array();
$key= \'Creation_views\';
$query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = \'$key\'";
$results = $wpdb->get_results($query);
var_dump($results);
if(!empty($results) && !is_null($results)){
foreach($results as $result){
foreach($result as $value){
$metaValues[] = $value;
}
}
}
//get_results returns null on failure
if(is_null($results) && WP_DEBUG == true){
$wpdb->show_errors();
$wpdb->print_error();
}
return $metaValues;
$values = get_meta_value_by_meta_key(\'Creation_views\');
$sum = array_sum(array_map(\'intval\', $values));
}
add_shortcode(\'Stats\',\'get_meta_value_by_meta_key\');