php - Sum All the Post Meta of Published posts of Current Logged in user

I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet. Aut

I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet.

  1. Author stats: How to show the current logged in user, the total views of all the published posts via shortcode. How can I handle this? I found an answer on WPSE for showing the total views but it doesn't add shortcode and gets views of all posts, I want views of all the published posts

  2. For representing the data, how to show the views of each and every author in a html table, not just current author, all the authors, just the author name and total post views. This can be useful for multi author sites and can be used to track performance of all the authors.

// 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);
    }
}

I tried to do a array of all the post ids of current user but I failed. I also tried to add a Author id, portion in this post meta to make easy. But none seemed to work.

Here's the current code implementation

 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');

I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet.

  1. Author stats: How to show the current logged in user, the total views of all the published posts via shortcode. How can I handle this? I found an answer on WPSE for showing the total views but it doesn't add shortcode and gets views of all posts, I want views of all the published posts

  2. For representing the data, how to show the views of each and every author in a html table, not just current author, all the authors, just the author name and total post views. This can be useful for multi author sites and can be used to track performance of all the authors.

// 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);
    }
}

I tried to do a array of all the post ids of current user but I failed. I also tried to add a Author id, portion in this post meta to make easy. But none seemed to work.

Here's the current code implementation

 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');
Share Improve this question edited Aug 11, 2019 at 3:35 Aditya Agarwal asked Aug 9, 2019 at 11:43 Aditya AgarwalAditya Agarwal 2764 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to create custom query, to get data from {$wpdb->prefix}postmeta depending on the meta_key and store the meta_values inside an array, then sum them.

So we can build a function like this:

/**
* 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());

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745252231a4618745.html

相关推荐

  • php - Sum All the Post Meta of Published posts of Current Logged in user

    I found the below code to count post views in WordPress. I needed 2 things from this, which I am not able to do yet. Aut

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信