How do I check if an article is popular this week?

I use this function to set and get post view of post in my WordPress

function wpb_set_post_views($postID) {
    $count_key = 'wpb_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);
function wpb_get_post_views($postID){
    $count_key = 'wpb_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.' view';
}

Please , how can I check if an article is popular within a week or a month in archive list.

For example, if the article in list was popular during the same week, a .popular_week class is added to the article.

like :


div class=loop
div class=articlearticle1/div 
div class=articlearticle2/div
div class=article popular_weekarticle3/div
div class=articlearticle4/div
/div

I hope the idea is clear

Topic post-meta wp-query Wordpress

Category Web


You can try this code may be useful for you.

<?php
add_action('wp_head', 'artical_count_post_visits');
function artical_count_post_visits() {
    if (is_single()){
        global $post;
        $views = get_post_meta($post->ID , 'article_views', true);
        if((date('D') != 'Sun')){ //weekly
            if ($views == ''){
                update_post_meta( $post->ID, 'article_views', '1');
            } else {
                $views_no = intval($views);
                update_post_meta( $post->ID, 'article_views', ++$views_no);
            }
        }else{
            update_post_meta($post->ID, 'article_views', '');
        }
    }
    //popular article set into option meta
    if((date('D') != 'Sun')){
        $blog_query = new WP_Query( array( 'post_type' => 'post','posts_per_page' => -1 ));
        if($blog_query->have_posts()){
            $previous_views = "";
            $max_views      = "";
            $article_max_id = "";
            while ($blog_query->have_posts() ) {
                $blog_query->the_post(); 
                $current_views= get_post_meta(get_the_ID(), "article_views", true);
                if($current_views > $previous_views){
                    $previous_views = $current_views;
                    $max_views      = $current_views;
                    $article_max_id = get_the_ID();
                }
            }
           if(!empty($article_max_id) && !empty($max_views)){
            update_option( 'popular_article_save', array($article_max_id => $max_views) );
           }
            wp_reset_postdata();
            wp_reset_query();
        }
    
    }else{
        update_option( 'popular_article_save', array() );
    }
}
?>

<?php
get_header(); ?>
<!-- archive list -->
<div class="loop">
    <?php
    if ( have_posts() ) {
        $popular   = get_option('popular_article_save');
        while ( have_posts() ) {
            the_post(); 
            $views     = get_post_meta(get_the_ID(), "article_views", true);
            $class    = "";
            if(!empty($popular)){
               foreach($popular as $key_id=>$max_views){
                    if($key_id == get_the_ID() && $max_views == $views && !empty($max_views)){
                      $class    = "popular_week";
                    }
                }
            }
            ?>
            <div class="article <?php echo $class; ?>" ><?php echo get_the_title();?></div>
            <?php
        } 
    } 
    ?>
</div>
<?php get_footer();?>

Short answer:

You can't from that code


Longer answer:

So the code above will simply increment a counter based on the last value the counter was on. There's no way to tell when that page-view or count starts from it's simply a number. You have no data on when that page view was counted or even last updated and certainly not how many of those counts occurred in the last week.

I've written about this before as it's not a simple topic but in a nutshell counting post views efficiently is a nightmare - by recording views and writing data back to that same data source you are removing your read-cache from the database query, every time a page is viewed your database will need to be read from fresh as a write has just written your post-meta value with a counter.

The most efficient way I've found of doing this is to use an external service like Google Analytics or Adobe Analytics then query their API periodically for your popularity data. Perhaps every 2 -3 or 6 - 12 hours (depending on your web traffic) and get your popularity data from a data source that already has it there. You're really re-inventing the wheel with page-views - big companies have already spent 000s on making this efficient.

If you want to do it yourself - strap-in as you'll be looking at multiple, separate database tables, custom indexing structure, cron-jobs to process lists of timestamps against pages and Ajax calls to your counter so server-side page caching can work effectively.

I've had to abandon the more popular page view counting plugins on the WP.org plugin repo as they were all dismally inefficient - adding overhead and page-load times that were unacceptable. If you're running a small site it probably won't matter - you should take something off the shelf and use that... but remember to add it to the top of the list to change when / if your site traffic grows.


Also, a warm welcome to WPSE @jimi-del don't forget to read up on our community guidelines.

How do I ask a good question?

Code of Conduct

What should I do when someone answers my question?

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.