On my last post, I wrote custom functions to display the post views in WordPress without any plugin. As promised, in this post I will show you how to display number of posts based on the post views count. This could be great way to display your popular posts in WordPress. Even though many user likes to display popular posts based on the total comment count of posts but we will focus on the post views. This is just to show you how you can take the advantage of having post views count on your site.
So, lets take a look at this snippet first.
<?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 3, 'meta_key' => 'views_count', 'orderby' => 'meta_value_num'); $posts = new WP_Query($args); if ($posts->have_posts()) : echo '<h3>Popular Posts</h3>'; echo '<ul>'; while ($posts->have_posts()) : $posts->the_post(); echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a>'; echo '<span>'.get_post_meta($post->ID,'views_count', true).'</span></li>'; endwhile; echo '</ul>'; endif; wp_reset_postdata(); ?>
That’s all. You can place this snippet anywhere on your site within a <div> element and style it any way you want.
In a simple sentence, we are querying our database to find few posts that meets certain criteria (Parameters). First of all, you need to understand how exactly WP_Query works. WordPress has pretty good documentation on this. So, read it first to understand this snippet.
At the very beginning of this snippet, we assigned a variable “$args” which holds different parameters within an array. Later on we are querying our database for posts that meets this criteria and running a loop to display the post title and the views count of those posts.
If you take a look at these parameter, you would find that I set the value for “post_type” as “post”. This would display post views count for default “posts” of WordPress. However, if you are keeping the post views count of “Custom Post Type”. You can assign them here as well like this:
'post_type' => array('post','custom_post_type'),
However, lets not forget that this would allow all posts (blog posts & custom post type) to compete each other to display the most viewed posts.
Now, I also used “posts_per_page” value to be 3 which means I want to display total 3 posts. You have all the freedom of the world to change it to your desired number. The usage of “meta_key” is probably the most important thing here. The value “views_count” is the name of the meta key that holds the post views count. Please follow the link that I have added on the first line of this post to better understand how and where exactly we got this meta key from. Lastly, I assigned “orderby” value to be “meta_value_num” which means posts would ordered based on highest views count. At this point, since I have all the necessary parameters I can simply pass it to the WP_Query class and run the loop. For each post, I also grabbed the post permalink, title, views (you can display post published date as well if you want) to give it a good look.
So, that’s all for today. I hope you find this post to be useful. If you have any question, feel free to drop me a line from the comment section below. Thanks.