Do you need to show fashionable posts primarily based on their views in WordPress?
By displaying your hottest WordPress weblog posts, you possibly can assist readers discover your finest and most useful content material. Plus, it will possibly encourage them to remain longer in your web site.
On this article, we’ll present you find out how to show your fashionable posts by views in WordPress.
Why Show Well-liked Posts by Views in WordPress?
When customers see your hottest WordPress weblog posts, they will uncover what content material their fellow readers like essentially the most. This manner, they will verify these articles out and take part on the dialog.
What’s extra, displaying your hottest content material in WordPress can increase 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, displaying 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 website can rank larger. Plus, this provides readers extra time to transform, like signing up in your e-newsletter.
Sadly, WordPress doesn’t have a built-in function or block to show your hottest posts by web page views. On this information, we are going to present you find out how 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
In the event you desire written directions, then simply preserve studying.
Technique 1: Show Well-liked WordPress Posts by Views With MonsterInsights
The simplest method to show fashionable posts by views in WordPress is with a plugin. There are literally many WordPress fashionable submit plugins obtainable, however in our opinion, your best option is MonsterInsights.
Utilized by 3 million+ web sites worldwide, MonsterInsights is the very best Google Analytics plugin for WordPress. Whereas its predominant performance is analytics, it additionally has a user-friendly function to point out your hottest posts.
First issues first, you’ll need to put in the MonsterInsights plugin and join it to your Google Analytics account. For extra particulars, see our step-by-step information on find out how to set up Google Analytics in WordPress for newcomers.
Select a Well-liked Posts Widget Theme
When you’ve activated and arrange the plugin, go to Insights » Well-liked Posts from the WordPress dashboard. Then, click on the ‘Well-liked Posts Widget’ menu merchandise.
Scrolling down, you’ll need to pick out 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.
It’s also possible to preview the theme in a Huge or Slim format. With the Huge format, you’ll see your posts listed below the web page or submit content material, whereas the Slim format shows the posts on the precise aspect of it, like a sidebar.
Subsequent, let’s scroll all the way down to the ‘Customise Design’ part. Right here, you possibly can modify the theme’s colours, font measurement, format, and submit depend to show.
The customization choices right here might range relying on the theme you employ. So, make sure you discover the completely different themes to see which one fits you finest.
Configure the Well-liked Posts Widget Habits
When you’ve configured the theme’s design, you should go to the Habits 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 possibly can select to make use of the design that you simply’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 possibly can select how the widget ought to select your hottest posts. Right here, decide ‘Curated.’
What it’s a must to do subsequent is allow the ‘Add High 5 Posts from Google Analytics’ possibility under. However earlier than doing that, you’ll need the MonsterInsights Dimensions Add-On and so as to add Submit sort as a brand new customized dimension.
This can let MonsterInsights select the preferred posts primarily based on Google Analytics knowledge.
To do that, it’s a must to obtain the MonsterInsights Dimensions Add-on and set up it as a plugin in WordPress. For step-by-step directions, try our information on find out how 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 completed that, choose ‘Submit sort’ within the dropdown menu.
This can allow MonsterInsights to trace the efficiency of your weblog posts and different customized submit sorts.
Now, simply return to Insights » Well-liked Posts and go to ‘Well-liked Posts Widget.’
Merely activate the ‘Add High 5 Posts from Google Analytics’ toggle button. Then, click on ‘Check Automated Posts’ to see if the setup was profitable.
If sure, then you will notice a hit message popup.
It ought to say: ‘Well-liked Posts knowledge may be fetched accurately. Please observe: relying on whenever you arrange the Customized Dimensions settings, it could take as much as 7 days to see related Well-liked Posts knowledge loading from Google Analytics.’
Shifting on to the following steps, now you can add a title that can seem above the widget. It may be one thing like ‘Verify Out Our Most Well-liked Posts’ or something related.
Under that, you possibly can select whether or not the widget must be displayed in all submit sorts, be excluded from particular posts, and/or seem in sure submit classes solely.
These settings are helpful in case you have customized submit sorts apart 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 mechanically, 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 Well-liked Posts Widget Routinely
Automated Placement is beneficial if you would like the widget to point out up on all of the posts that match the Habits settings you’ve set beforehand.
For this, all it’s a must to do is activate the Automated Placement button.
The draw back with this selection is that you simply can’t alter the widget’s design primarily based on what appears finest on the submit or web page. In the event you want this sort of performance, then you possibly can strive the following technique.
Embed the Well-liked 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 Well-liked Posts block.
Within the Block settings panel, you will notice that there are alternatives to alter the widget’s theme, font measurement, colours, title, format, and submit depend.
These settings are much like those we’ve seen within the MonsterInsights plugin.
The one distinction is you possibly can’t change which posts get chosen within the widget.
That stated, within the Habits Settings, it’s attainable to solely present posts from sure classes. This manner, you may make the favored submit selections extra tailor-made to your wants.
As soon as you’re proud of the block settings, simply click on the ‘Replace’ button within the submit and web page editor.
Right here’s what the Well-liked Posts block appears like on our take a look at web site:
Embed the Well-liked 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 possibly can add the Well-liked Posts widget to it.
All it’s a must to do is go to Look » Widgets. Then, click on the ‘+’ add widget button within the Sidebar space and search for the Well-liked Posts – MonsterInsights widget.
Like within the earlier technique, the widget contains related settings to customise your theme’s look.
What’s completely different is that the interface is barely trickier to make use of. As you alter the settings, you possibly can’t see what the widget really appears like. You’ll have to click on on a distinct widget or one other space to preview it.
In case you are proud of how the widget appears, simply click on ‘Replace’ within the high proper nook.
Be certain that to go to your web site to verify if the sidebar appears proper.
Right here’s what ours appears like:
Embed the Well-liked 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 Well-liked 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 data, try our information on find out how to add shortcodes in WordPress.
Technique 2: Show Well-liked WordPress Posts by Views With Code
The MonsterInsights technique is the best method to show your hottest and finest content material by views in WordPress. However in case you are snug with coding, then you may as well use code to point out 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 together with your WordPress recordsdata. 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, it is advisable set up WPCode in WordPress. A free model of WPCode is out there, however we are going to use the Professional model, because it comes with the options we have to insert the code in the precise areas.
For a step-by-step set up information, learn our article on find out how to set up a WordPress plugin.
Add WordPress Submit Views Counter Perform
Upon the plugin’s activation, it is advisable 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 possibly can identify it one thing like ‘WordPress Submit Views Counter Perform.’ It is because this primary code’s function 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 possibly can copy and paste the next code under:
$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 Technique and ‘Run In all places’ 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 aspect of the web page and make the code lively.
Then, click on ‘Save Snippet.’
At this stage, it is advisable add one other new customized code snippet. You possibly can identify this one ‘Monitor Submit Views Perform Name.’
This code will name the perform above and activate it in your single submit pages.
When you’ve completed that, copy and paste the next snippet:
What’s completely different about this code snippet is you’ll select the placement ‘Insert After Submit’ as a result of it’s imagined to run in your single submit pages.
If you find yourself completed, simply make the code lively and put it aside.
Insert WordPress Submit Views Monitoring Hook
Now, it is advisable add a brand new code snippet that checks if the present web page is a single submit. In that case, it can 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 identify this snippet ‘WordPress Submit Views Monitoring Hook.’ Then, insert the next strains of code:
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 system might not work by default. You could possibly use the fragment caching function that’s supplied by some caching plugins like W3 Whole Cache to make it work.
Add Capabilities to Retrieve Submit Views Rely and Show Most Well-liked Posts With View Counts
We at the moment are within the closing steps of this information. You want to add a code snippet that retrieves and codecs the variety of views for a given WordPress submit. This can turn out to be useful later when displaying the record of most seen posts.
You possibly can identify this code as ‘Retrieve Submit Views Rely Perform’ and set the placement to ‘Run In all places.’
Right here is the code snippet:
$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, make sure you make the code lively in WPCode and put it aside after.
When you’ve completed that, create a brand new code snippet once more and identify it ‘Show Most Well-liked 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:
$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 Well-liked 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, be sure that to pick out ‘Insert After Submit’ for the Location. Then, simply make the code lively and save the snippet.
All that’s left to do now could be go to one in every of your weblog posts and see if the code works. Here’s what the preferred posts by views part appear like on our take a look at WordPress weblog:
Skilled Tip: To see in case your code works with out completely altering your website, you possibly can strive WPCode’s Testing Mode.
This function permits you to add your codes like regular and solely you possibly can see what they appear like on the entrance finish. On the opposite aspect, your customers will work together together with your web site as regular.
We hope this text helped you discover ways to show fashionable posts by views in WordPress. You might also need to try our article on find out how to show fashionable merchandise in WooCommerce and our newbie’s information to WordPress customized fields.
In the event you favored this text, then please subscribe to our YouTube Channel for WordPress video tutorials. It’s also possible to discover us on Twitter and Fb.