Displaying daily post view count of an user

I use custom code to get the post views count of authors in the front end. I can display the sum total of all the post views of particular author. They are all together post views count and doesn’t relate to any time frame or periods.

However, what I’m also trying to achieve is – views count of particular period. For example: Show post views count of Author ‘A’ in last 24 hours or last 15 days..

For getting total count I use the following code:

Functions.php

function getinsharepostview($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";
}
return $count.' ';
}

function setinsharepostview($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);
  }
}

remove_action( ‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0);

Then to get the total views count of logged in author, I use this in the template page.

$author_id = get_current_user_id(); 
$author_posts = get_posts( array('author' => $author_id ) );
$counter = 0;
foreach ( $author_posts as $post ){
$views = getinsharepostview($post->ID);
$counter += $views;}
echo "{$counter}";

Thanks in advance

Read more here: Displaying daily post view count of an user

Leave a Reply

Your email address will not be published. Required fields are marked *