0

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

1 Answer 1

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());
19
  • Well that seems the right idea, i will surely test and get back to you in some time... Aug 9, 2019 at 15:27
  • seems like it doesn't work. i added a shortcode, and then added function get_meta_value_by_meta_key($key, $values, $sum) and then put $sum and $ values at end of function. on frontend the shortcode returns the word 'array' rather than having the data or sum... Aug 11, 2019 at 2:19
  • i assume that the problem is $metaValues = array(); because the array has not been given the data Aug 11, 2019 at 2:24
  • have you tried the code directly without the shortcode
    – Makiomar
    Aug 11, 2019 at 2:50
  • well i tried to run it in after content of posts and it didn't run there too. so i have tried at shortcode as well as a filter.. Aug 11, 2019 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.