Show Popular Posts Without Plugins

Show Popular Posts Without Plugins: This one is a little trickier. However, if you are not too keen on installing an extra plugin to showcase popular posts (say, you have limited server memory or disk space), follow this snippet. Remember lots of plugins is not always a good thing. They don’t always play nice and can crash your site.

Paste the following in functions.php:


function count_post_visits() {
    if( is_single() ) {
        global $post;
        $views = get_post_meta( $post->ID, 'my_post_viewed', true );
        if( $views == '' ) {
            update_post_meta( $post->ID, 'my_post_viewed', '1' );   
        } else {
            $views_no = intval( $views );
            update_post_meta( $post->ID, 'my_post_viewed', ++$views_no );
        }
    }
}
add_action( 'wp_head', 'count_post_visits' );

 

Thereafter, paste the following wherever in the template files that you wish to display the popular posts:


$popular_posts_args = array(
 'posts_per_page' => 3,
 'meta_key' => 'my_post_viewed',
 'orderby' => 'meta_value_num',
 'order'=> 'DESC'
);
 
$popular_posts_loop = new WP_Query( $popular_posts_args );
 
while( $popular_posts_loop->have_posts() ):
 $popular_posts_loop->the_post();
 // Loop continues
endwhile;
wp_reset_query();

Related Posts:

Allow Contributors to Upload Images in WordPress

How do I install plugins in WordPress?

Disable the Admin Bar in WordPress

Create a PayPal Donation Shortcode – WordPress

FAQ Hacked WordPress Website

Change the Author Permalink Structure

Introduction to Python Programming Language

Using the Current Year in Your Posts – WordPress

Redirect New Registered Users to a Specific Page – WordPress

How do I start a WordPress blog? (hosting wordpress)

Should I Choose a Hosted or Non-hosted Blogging Platform?

The background-color CSS property

Introduction to JavaScript – Properties

Show Post Thumbnails in RSS Feed – WordPress

How to Update: Automatically create media_buttons for shortcode selection

Choose your preferred blog CMS platform

Paginate Your WordPress Site Without Plugins

WordPress by the Numbers Facts

Automatically Link to Twitter Usernames in Content

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.