Do you wish to show well-liked posts primarily based on their views in WordPress?
By exhibiting your hottest WordPress weblog posts, you may assist readers discover your greatest and most dear content material. Plus, it could possibly encourage them to remain longer in your web site.
On this article, we’ll present you the best way to show your well-liked posts by views in WordPress.
Why Show Standard Posts by Views in WordPress?
When customers see your hottest WordPress weblog posts, they will uncover what content material their fellow readers like probably the most. This manner, they will test these articles out and take part on the dialog.
What’s extra, displaying your hottest content material in WordPress can enhance your social proof. It’s like telling new customers that, “Hey, different folks have discovered these posts actually useful, and also you would possibly too!”
Lastly, exhibiting your trending weblog posts could make folks keep longer in your web site and discover extra of your content material.
The extra your consumer engagement will increase, the extra Google thinks your content material is nice and essential. In flip, your web site can rank larger. Plus, this provides readers extra time to transform, like signing up in your publication.
Sadly, WordPress doesn’t have a built-in characteristic or block to show your hottest posts by web page views. On this information, we are going to present you the best way to do precisely that with a plugin and with code.
Simply click on on one of many fast hyperlinks under to leap straight to your most popular technique:
Video Tutorial
When you favor written directions, then simply hold studying.
Methodology 1: Show Standard WordPress Posts by Views With MonsterInsights
The best approach to show well-liked posts by views in WordPress is with a plugin. There are literally many WordPress well-liked submit plugins out there, however in our opinion, the only option is MonsterInsights.
Utilized by 3 million+ web sites worldwide, MonsterInsights is the very best Google Analytics plugin for WordPress. Whereas its primary performance is analytics, it additionally has a user-friendly characteristic to indicate your hottest posts.
First issues first, you will want to put in the MonsterInsights plugin and join it to your Google Analytics account. For extra particulars, see our step-by-step information on the best way to set up Google Analytics in WordPress for freshmen.
Select a Standard Posts Widget Theme
When you’ve activated and arrange the plugin, go to Insights » Standard Posts from the WordPress dashboard. Then, click on the ‘Standard Posts Widget’ menu merchandise.
Scrolling down, you will want to pick a theme to show the favored posts widget. There are numerous choices.
Some embody a featured picture for the submit, whereas some are extra minimal-looking.
If you wish to see what the theme appears like earlier than making your alternative, simply transfer down the web page to the Theme Preview part.
You can even preview the theme in a Extensive or Slim format. With the Extensive format, you’ll see your posts listed beneath the web page or submit content material, whereas the Slim format shows the posts on the suitable facet of it, like a sidebar.
Subsequent, let’s scroll all the way down to the ‘Customise Design’ part. Right here, you may modify the theme’s colours, font dimension, format, and submit depend to show.
The customization choices right here might differ relying on the theme you employ. So, you’ll want to discover the totally different themes to see which one fits you greatest.
Configure the Standard Posts Widget Conduct
When you’ve configured the theme’s design, you have to go to the Conduct part. That is the place you’ll management how the favored posts widget will seem in your WordPress web site.
Within the Widget Styling settings, you may select to make use of the design that you just’ve made above or decide to not fashion the widget in any respect. With the second possibility, the widget will comply with your WordPress web site theme’s CSS.
Then, you may select how the widget ought to select your hottest posts. Right here, choose ‘Curated.’
What you must do subsequent is allow the ‘Add Prime 5 Posts from Google Analytics’ possibility under. However earlier than doing that, you will want the MonsterInsights Dimensions Add-On and so as to add Submit sort as a brand new customized dimension.
It will let MonsterInsights select the preferred posts primarily based on Google Analytics information.
To do that, you must obtain the MonsterInsights Dimensions Add-on and set up it as a plugin in WordPress. For step-by-step directions, take a look at our information on the best way to set up a WordPress plugin.
Subsequent, go to Insights » Settings and change to the ‘Conversions’ tab. After that, click on ‘Add New Customized Dimension.’
When you’ve executed that, choose ‘Submit sort’ within the dropdown menu.
It will allow MonsterInsights to trace the efficiency of your weblog posts and different customized submit sorts.
Now, simply return to Insights » Standard Posts and go to ‘Standard Posts Widget.’
Merely activate the ‘Add Prime 5 Posts from Google Analytics’ toggle button. Then, click on ‘Take a look at Automated Posts’ to see if the setup was profitable.
If sure, then you will notice successful message popup.
It ought to say: ‘Standard Posts information might be fetched appropriately. Please notice: relying on whenever you arrange the Customized Dimensions settings, it might take as much as 7 days to see related Standard Posts information loading from Google Analytics.’
Shifting on to the subsequent steps, now you can add a title that can seem above the widget. It may be one thing like ‘Test Out Our Most Standard Posts’ or something comparable.
Beneath that, you may select whether or not the widget needs to be displayed in all submit sorts, be excluded from particular posts, and/or seem in sure submit classes solely.
These settings are helpful when you’ve got customized submit sorts aside from weblog posts and really feel like the favored posts widget will look irrelevant there.
One of many final steps is to embed the favored posts widget. There are a number of choices: inserting it robotically, utilizing a Gutenberg block, including it as a sidebar widget, or utilizing a shortcode.
Let’s undergo every possibility one after the other.
Embed the Standard Posts Widget Mechanically
Automated Placement is beneficial if you would like the widget to indicate up on all of the posts that match the Conduct settings you’ve set beforehand.
For this, all you must do is activate the Automated Placement button.
The draw back with this selection is that you just can’t alter the widget’s design primarily based on what appears greatest on the submit or web page. When you want one of these performance, then you may strive the subsequent technique.
Embed the Standard Posts Block to a Web page or Submit
If you wish to have extra management over how the widget appears on a web page or submit, then this technique is for you.
First, go to the block editor of the submit or web page the place you need the widget to be. After that, click on the ‘+’ add block button anyplace within the editor and search for the Standard Posts block.
Within the Block settings panel, you will notice that there are alternatives to vary the widget’s theme, font dimension, colours, title, format, and submit depend.
These settings are much like those we’ve seen within the MonsterInsights plugin.
The one distinction is you may’t change which posts get chosen within the widget.
That mentioned, within the Conduct Settings, it’s attainable to solely present posts from sure classes. This manner, you can also make the favored submit decisions extra tailor-made to your wants.
As soon as you’re pleased with the block settings, simply click on the ‘Replace’ button within the submit and web page editor.
Right here’s what the Standard Posts block appears like on our take a look at web site:
Embed the Standard Posts Widget right into a Sidebar
In case you are utilizing a non-block WordPress theme that has a widget-ready sidebar space, then you may add the Standard Posts widget to it.
All you must do is go to Look » Widgets. Then, click on the ‘+’ add widget button within the Sidebar space and search for the Standard Posts – MonsterInsights widget.
Like within the earlier technique, the widget contains comparable settings to customise your theme’s look.
What’s totally different is that the interface is barely trickier to make use of. As you modify the settings, you may’t see what the widget truly appears like. You’ll have to click on on a unique widget or one other space to preview it.
In case you are pleased with how the widget appears, simply click on ‘Replace’ within the prime proper nook.
Ensure to go to your web site to test if the sidebar appears proper.
Right here’s what ours appears like:
Embed the Standard Posts Widget With a Shortcode
The final possibility is to make use of a shortcode. This technique is beneficial if the above choices don’t work in your particular use case.
Within the Standard Posts Widget tab, scroll all the way in which all the way down to the ‘Embed Choices’ part. Then, select ‘Show utilizing a Shortcode’ and click on the ‘Copy Shortcode’ button.
After that, merely place the shortcode wherever you want. For extra info, take a look at our information on the best way to add shortcodes in WordPress.
Methodology 2: Show Standard WordPress Posts by Views With Code
The MonsterInsights technique is the simplest approach to show your hottest and greatest content material by views in WordPress. However in case you are snug with coding, then you may as well use code to indicate your hottest WordPress posts.
For this technique, we suggest utilizing a code snippets plugin like WPCode, which is what we are going to use on this information.
WPCode makes it secure to insert customized code with out immediately interacting along with your WordPress information. Plus, since this technique requires utilizing a number of code snippets, the plugin will make managing and monitoring all of them a lot simpler.
First, you want to set up WPCode in WordPress. A free version of WPCode is offered, however we are going to use the Pro version, because it comes with the options we have to insert the code in the suitable areas.
For a step-by-step set up information, learn our article on the best way to set up a WordPress plugin.
Add WordPress Submit Views Counter Perform
Upon the plugin’s activation, you want to go to Code Snippets » + Add Snippet from the dashboard. Then, hover over ‘Add Your Customized Code (New Snippet)’ and click on ‘Use snippet.’
Now, you’re contained in the WPCode code snippet editor. Earlier than inserting any code, go forward and add a title for the snippet.
For this primary code, you may title it one thing like ‘WordPress Submit Views Counter Perform.’ It’s because this primary code’s goal is to create a perform for counting submit views in WordPress.
Subsequent, change the Code Sort to ‘PHP Snippet.’ Notice that each code snippet you’ll have to add from this text is in PHP.
After that, you may copy and paste the next code under:
perform wpb_set_post_views($postID) {
$count_key = 'wpb_post_views_count';
$depend = get_post_meta($postID, $count_key, true);
if($depend==''){
$depend = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$depend++;
update_post_meta($postID, $count_key, $depend);
}
}
//Do away with prefetching to maintain the depend correct
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Now, simply scroll all the way down to the ‘Insertion’ part and choose ‘Auto Insert’ for the Insert Methodology and ‘Run In every single place’ for the Location. This works the identical as inserting the code into your theme’s features.php file.
After that, navigate to the highest proper facet of the web page and make the code energetic.
Then, click on ‘Save Snippet.’
At this stage, you want to add one other new customized code snippet. You possibly can title this one ‘Observe Submit Views Perform Name.’
This code will name the perform above and activate it in your single submit pages.
When you’ve executed that, copy and paste the next snippet:
wpb_set_post_views(get_the_ID());
What’s totally different about this code snippet is you’ll select the placement ‘Insert After Submit’ as a result of it’s speculated to run in your single submit pages.
If you end up executed, simply make the code energetic and reserve it.
Insert WordPress Submit Views Monitoring Hook
Now, you want to add a brand new code snippet that checks if the present web page is a single submit. If that’s the case, it’s going to name the WordPress Submit Views Counter Perform to trace and replace the submit’s view depend.
This manner, each time a consumer visits the submit, its view depend will get up to date.
Let’s title this snippet ‘WordPress Submit Views Monitoring Hook.’ Then, insert the next traces of code:
perform wpb_track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
international $submit;
$post_id = $post->ID;
}
wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');
You possibly can select the insert technique as ‘Auto Insert’ and the placement as ‘Frontend Solely.’
Identical with the earlier steps, simply go forward and activate the code and save the snippet.
Notice: In case you are utilizing a caching plugin, then this method might not work by default. You can use the fragment caching characteristic that’s provided by some caching plugins like W3 Complete Cache to make it work.
Add Capabilities to Retrieve Submit Views Rely and Show Most Standard Posts With View Counts
We are actually within the closing steps of this information. It’s good to add a code snippet that retrieves and codecs the variety of views for a given WordPress submit. It will turn out to be useful later when displaying the listing of most considered posts.
You possibly can title this code as ‘Retrieve Submit Views Rely Perform’ and set the placement to ‘Run In every single place.’
Right here is the code snippet:
perform wpb_get_post_views($postID){
$count_key = 'wpb_post_views_count';
$depend = get_post_meta($postID, $count_key, true);
if($depend==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $depend.' Views';
}
Once more, you’ll want to make the code energetic in WPCode and reserve it after.
When you’ve executed that, create a brand new code snippet once more and title it ‘Show Most Standard Posts with View Counts.’ That is the code that can present your hottest posts on the backside of your weblog posts.
Within the Code Preview field, insert the next snippet:
// Customise the question parameters as wanted
$popularpost = new WP_Query( array(
'posts_per_page' => 3, // Be happy to regulate the variety of posts to show
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
?>
<div class="popular-posts">
<h2>See Our Most Standard Posts</h2> <!-- Be happy to customise the title -->
<ul>
<?php whereas ( $popularpost->have_posts() ) : $popularpost->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
- <?php echo wpb_get_post_views(get_the_ID()); ?>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php wp_reset_postdata();
As famous within the code, be at liberty to regulate the variety of posts to show and customise the title for this part.
Within the WPCode Insertion part, make sure that to pick ‘Insert After Submit’ for the Location. Then, simply make the code energetic and save the snippet.
All that’s left to do now’s go to one in all your weblog posts and see if the code works. Here’s what the preferred posts by views part appear to be on our take a look at WordPress weblog:
Professional Tip: To see in case your code works with out completely altering your web site, you may strive WPCode’s Testing Mode.
This characteristic helps you to add your codes like regular and solely you may see what they appear to be on the entrance finish. On the opposite facet, your customers will work together along with your web site as regular.
We hope this text helped you discover ways to show well-liked posts by views in WordPress. You might also wish to take a look at our article on the best way to show well-liked merchandise in WooCommerce and our newbie’s information to WordPress customized fields.
When you appreciated this text, then please subscribe to our YouTube Channel for WordPress video tutorials. You can even discover us on Twitter and Facebook.
Leave a comment