将当前登录用户已发布帖子的所有帖子元求和

时间:2019-08-09 作者:Aditya Agarwal

我在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\');

1 个回复
SO网友:Makiomar

您必须创建自定义查询,才能从{$wpdb->prefix}postmeta 取决于meta_key 并存储meta_value,然后求和。

因此,我们可以构建如下函数:

/**
* Query meta values by meta key.
* 
* @return int Returns sum of meta values
*/

function get_meta_value_by_meta_key(){
    global $wpdb;

    $metaValues = array();

    $query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = \'Creation_Views\'";

    $results = $wpdb->get_results($query);

    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();
    }
    $sum = array_sum(array_map(\'intval\', $metaValues));
    return $sum;

   }
 add_shortcode(\'Stats\', get_meta_value_by_meta_key());

相关推荐

Vagrant Access index.php

我相信这是一个合适的地方。首先,我安装了VirtualBox和Vagrant。在那之后,我将cd刻录到以下内容:https://github.com/LearnWebCode/vagrant-lamp (下载了zip,提取了文件夹)然后在运行vagrant up之后,我到达了Congratulations 通过IP分页http://192.168.56.101/之后,我在hosts文件中设置了一个易于阅读的URL,并可以运行它,但它会导致出现祝贺页面,而不是索引。php的Hello World。你知道怎么