We’ll teach you how to show or display popular posts or articles by views on WordPress sites without requiring a third-party plugin in this fast guide.
WordPress is the most popular content management system; only WordPress allows you to create a website in less time. It has a large number of plugins and ready-made themes, as well as excellent tutorials and support.
Custom plugins and themes, on the other hand, include a lot of code, therefore it’s best to build any special feature without using the plugin.
We’ll show you how to use WordPress custom functions and methods to display popular posts by views without requiring an external plugin, and we’ll do it with only a few lines of code.
The fundamental job of a site owner is to increase user engagement; there are several strategies and methods that may be used to attain this goal.
One method is to display your site’s most popular post to visitors, which you may do in a sidebar or immediately before the piece ends.
But do you know how to expose your site’s most popular posts to visitors? That is precisely why we are here to assist you.
We’ll go through some of the custom functions that may be used to make a popular post by views widget in WordPress without the use of a plugin.
After reading this fast lesson, you’ll have a solid understanding of how to show the most popular posts in WordPress by views without using a plugin.
However, because you don’t know how large the utilized plugin is, this procedure has a direct influence on your site’s performance.
To address the issue of site performance, we developed a simple tool to show posts by views in WordPress.
Table of Contents
Requirements
- PHP 8.1 or above
- WordPress 5.7 or greater
- MySQL 8.0 or above
Count of Post Views in the Store
We must first go to the functions.php file, and then to the main functions file,
where we must save the views count for each and every post or article in a custom field.
As a result, be sure to include the following code at the bottom of the function file; alternatively, you may include the suggested code in the functions.php configuration file of a child theme or the main theme.
<?php
/**
* WP Jugaads functions and definitions
*
*/
function wpjugaads_set_post_views($post_id) {
$count_key = 'wp_post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
Inside the wpjugaads_set_post_views() custom function, we passed the $post_id variable which represents the current post id. The wp_post_views_count meta key is keeping an eye on views count for every post.
We gave the $post_id variable, which represents the current post id, to the wpjugaads_set_post_views() custom function. The wp_post_views_count meta key keeps track of how many times each post has been viewed.
We also retrieved the current post count for each view created by a user.
Nonetheless, we have built the routines so that if no count detectes, the meta key is removed and the meta key count is set to zero.
Also Read these:
Count Times of Seen of Posts:
You can also retrieve the current post views by using the following method and inserting it into the single post loop query.
wpjugaads_set_post_views(get_the_ID())
The following code can also be included in the functions.php config file:
The pre-fetching problem is solved by the adjacent_posts_rel_link_wp head filter.
Simply put, it will correctly and precisely report the number of post views.
function wpjugaads_track_post_views ($post_id) {
if ( !is_single() )
return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
wpjugaads_set_post_views($post_id);
}
add_action( 'wp_head', 'wpjugaads_track_post_views');
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
In general, you should put this code where you want to list popular posts by views, which may be a sidebar or a widget.
<?php
$popularpostbyview = array(
'meta_key' => 'wp_post_views_count', // set custom meta key
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 4
);
// Invoke the query
$prime_posts = new WP_Query( $popularpostbyview );
if ( $prime_posts->have_posts() ) :?>
<ul>
<?php
while ( $prime_posts->have_posts() ) : $prime_posts->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php
endif;
Finally, we’ve arrived at the conclusion of this example; let me go through the code below one by one.
The features of the $popularpostbyview variable include a custom meta key,
An order by a prop to recall the current post order, and posts per page, which displays the total number of popular posts.
So, finally, It’s done, You can imagine how easy it was, hope So you enjoyed this article,
If you enjoyed then let us know in the comment section down below.
Motivate Us for More Helpful Guides for you.