WordPress
October 13, 2021

Display Most Popular Posts by Views in WordPress

1. Store Post Views Count

You will find out how to display popular posts by views in WordPress without an external plugin, and we will create this feature using few lines of code that consists of WordPress custom function and methods.

So, make sure to place following code at function file, you may place the suggested code in your current themes functions.php file.

Inside the wc_set_post_views() custom function, we passed the $postID variable which represents the current post id. The wc_post_views_count meta key is keeping an eye on views count for every post.

We can also place the following code inside the functions.php config file, the adjacent_posts_rel_link_wp_head is solving the pre-fetching issue. In simple terms it will display post views count properly and accurately.

ID;
}
wc_set_post_views($post_id);
}

?>

Show Popular Posts by View in WordPress

Theoretically, you have to place this code where you will show popular posts by views, it might be sidebar, or any widget.

2. Get Current Post View Count

Furthermore, you may use the following function and place within the single post loop query to get the current post views.

3. Show Popular Posts by View in WordPress

‘post’,
‘orderby’ => ‘meta_value_num’, // set custom meta key
‘post_status’ => ‘publish’,
‘order’ => ‘ASC’,
‘meta_key’ => ‘wc_post_views_count’,
‘posts_per_page’ => 4
);
$popularpost = new WP_Query( $popular_postby_view );
if ( $popularpost-> have_posts() ) : ?>

have_posts() ) : global $post; $popularpost->the_post(); ?>

Related Post


Create Custom Template for Child Categories in WordPress

Create different template only for child category put following code in your current themes functions.php file

Remove Version From CSS & JS in WordPress

Let’s start with removing versions from style and scripts. To do so, add the code below in your active theme’s functions.php file. After adding script on functions.php file you can view the source of any pages version should be removed from all your styles and scripts. You can also remove version from WordPress Head and […]

WooCommerce Redirect to Custom Thank you Page

The below code allows you to redirect to a custom page after the user has successfully completed a purchase when using WooCommerce plugin. Simply add the below code to the active child theme function.php and replace the URL in the function wp_redirect() to the desired page.

How to Create Custom Elements Using Visual Composer API

When we are making site with visual composer some time not possible to built some designs using visual composer elements. So at that time we need to use HTML element for built designs. But visual composer API provide facility to built own custom element for some special requirements and its very easy. We can built […]

How To Create Meta Boxes in WordPress

A meta box is a draggable box displayed in the post editing screen in the backend of WordPress. Users can select or enter extra information in meta boxes addition to the content in the main post editing area. There are two types of data you can enter in meta boxes: metadata (i.e. custom fields), and […]

Custom Pagination in WordPress

For custom pagination Add Following code in your current themes functions.php file.


Leave a Reply

Your email address will not be published. Required fields are marked *