Do you want to display popular posts by views in WordPress?
Displaying your popular posts can help you generate more traffic, keep visitors on your site longer, and build social proof.
In this article, we’ll show you how to display your popular posts by views in WordPress, both with and without a plugin.
Why Display Popular Posts by Views in WordPress?
Sometimes it can be hard for your visitors to find your best content. Even your most popular articles can get lost when you have thousands of blog posts.
Displaying your most popular posts lets you show your most popular articles anywhere across your WordPress blog.
Your popular posts are the most successful pieces of content for a reason. By displaying these to your visitors, you’ll build trust, improve social proof, and ensure that your visitors stay on your website longer.
When your visitors stay on your WordPress website longer, this gives you more time to convince them to make a purchase, join your email newsletter, or take another action.
With that said, let’s take a look at how to simply display popular posts by views in WordPress using 2 methods.
Click on the quick link to jump straight to your preferred method:
Method 1: Display Popular Posts by Views With a Plugin in WordPress
There are many WordPress popular posts plugins that you can use to display your most popular content, but the easiest plugin to use is MonsterInsights.
MonsterInsights is the best analytics solution for WordPress used by over 3 million websites. It lets you simply display your popular posts anywhere on your WordPress site.
You can also use the Inline Popular Posts feature to show your popular posts directly within your content.
First thing you need to do is install the plugin. For more details, see our step by step guide on how to install Google Analytics in WordPress for beginners.
Note: there is a free version of MonsterInsights available, but we’ll be using the pro version since it includes the popular post feature.
Upon activation and set up, go to Insights » Popular Posts and then click the ‘Popular Posts Widget’ menu item.
On this screen, you can select the popular post style you want to use. This will control the appearance of your popular posts.
There are a lot of additional customization options as well.
For example, under the ‘Theme Preview’ meta box, you can display your popular posts in a ‘Wide’ format below your content, or on the right hand side of your page with the ‘Narrow’ option.
Next, you can change the color and size of the post title, author, and date.
The ‘Widget-Layout Options’ menu will change the the number of columns that are displayed. There are additional display options you can customize on this screen as well.
MonsterInsights will automatically save all settings after you make a change.
Once you’ve customized the appearance of your popular posts, you’ll have a few different methods for adding them to WordPress.
In the ‘Embed Options’ meta box, there are 4 different display options. You can even use multiple display options together. The simplest way is turning on the ‘Automatic Placement’ toggle.
You can also display popular posts using Gutenberg Blocks in the new WordPress editor, with a shortcode, or by adding the widget to a sidebar.
To display your popular posts using Gutenberg Blocks open up a post or page you want to edit.
After that, click the ‘Add Block’ icon.
Search for ‘popular posts’ in the search bar and then choose the ‘Popular Posts’ or ‘Inline Popular Posts’ option.
Then, in the right hand sidebar, you can further customize the appearance of your popular posts.
The settings are similar to the settings from the MonsterInsights plugin menu we highlighted above.
After you’ve finished adding and customizing the appearance of your popular posts, make sure you click ‘Publish’ or ‘Update’ to save your changes.
Now, your visitors will see your popular posts when they visit your website.
Method 2: Display Popular Posts by Views Without a Plugin in WordPress
If you don’t want to use a plugin, or you’re already using too many plugins, then you can use this code method.
There are some downsides to using this method. First, it involves adding code to WordPress, and it’s not beginner friendly.
Second, the code method isn’t as performance optimized as MonsterInsights plugin, so it will increase server load and can slow down your site if you have a lot of content.
With that said, let’s take a look at how to add popular posts in WordPress without a plugin.
In this method, you’ll need to add code to your WordPress files. If you haven’t done this before, then check out our beginner’s guide to pasting snippets from the web into WordPress.
Now that you know how to add code in WordPress, let’s go ahead and add the following code to your functions.php file, in a site-specific plugin, or by using a code snippets plugin.
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); } } //To keep the count accurate, lets get rid of prefetching remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
The code above will detect post view counts and store it as a custom field for each post.
Once you add that function to WordPress, you need to call the function on your single post pages. Now, you need to tell the function which post gets credit for the views.
To do this, copy and paste the following code inside your single post loop.
wpb_set_post_views(get_the_ID());
If you are using a child theme, or you just want to make things easy for yourself, then you should simply add the tracker in your header by using wp_head hook.
To do this paste the following code in your theme’s functions.php file or the site-specific plugin (as shown above):
function wpb_track_post_views ($post_id) { if ( !is_single() ) return; if ( empty ( $post_id) ) { global $post; $post_id = $post->ID; } wpb_set_post_views($post_id); } add_action( 'wp_head', 'wpb_track_post_views');
Once you have placed this, every time a user visits the post, the custom field will be updated.
Note: If you are using a caching plugin, then this technique will not work by default. You could use Fragmented Caching feature that’s offered by some advanced caching plugins to bypass the caching plugins.
Now, you can do all sort of cool stuff such as display post view count, or sort posts by view count. Let’s take a look at how to do some of these cool things.
You can display the post view count on your single post pages, often next to the comment count, or your social share buttons.
To do this, add the following in your theme’s functions.php file or the site-specific plugin (highlighted above).
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.' Views'; }
Then inside your post loop add the following code:
wpb_get_post_views(get_the_ID());
If you want to sort the posts by view count, then you can do so easily by using the the wp_query post_meta parameter.
The most basic example loop query would look like this:
<?php $popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post(); the_title(); endwhile; ?>
To add other WP_Query parameters such as time range, refer to the WP_Query page in the Developers Handbook.
We hoped this article helped you learn how to display popular posts by views in WordPress. You may also want to see our guide on how to improve your WordPress SEO rankings, and our expert picks of the must have WordPress plugins for business websites.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Source: WPBeginner