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.



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

‘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

how to Upload WordPress Custom Meta Box Image

Hi, here i implement multiple image upload custom fields (WordPress custom meta box image upload ) to page post type if you want it for another post type just change post type name. Add the following code to your current themes function.php file. Go to your pages from WordPress admin and check that multiple image […]

Add Recaptcha in Custom Form in WordPress

You have to add recaptcha external js so use following function in your current themes functions.php file Set Recaptcha element in your form. //Custom Form Name: Put below js in your current themes custom.js file. //Captcha validation script jQuery(document).ready(function($) { $(document).on(‘submit’,’.wc-captcha-form’,function(e){ if(grecaptcha.getResponse() == “”) { e.preventDefault(); $(‘.captcha-error’).text(“Please verify captcha”); } else { $(‘.captcha-error’).text(“”); } }); […]

Redirect to Checkout after add to cart WooCommerce

Redirecting the user to the checkout page when they’ve added a product to their cart. This may come in handy if you only sell one product / one product per time. The filter woocommerce_add_to_cart_redirect makes it easy to redirect user to custom page. Code goes in function.php file of your active child theme (or active […]

Enable SVG Support in WordPress

For Enable svg support you’ll need to add the following code to your current themes functions.php file.

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.

Display Popular Tags in WordPress

you’ll need to add following code to your current themes functions.php file.

Leave a Reply

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