Monday, November 30, 2020
Home Blogging How to Display Popular Posts by Views in WordPress without a Plugin

How to Display Popular Posts by Views in WordPress without a Plugin

Prior to now now we have proven you methods to create a popular posts tabber in WordPress utilizing a plugin. That plugin works nice out the field for tabbers. Nonetheless, we wished extra customization in our format, so we determined to do it with no plugin. On this article, we are going to present you methods to monitor and show popular posts by views in WordPress with out utilizing any plugins.

An instance of our customized popular posts show is proven within the screenshot beneath:

Very first thing we have to do is create a perform that can detect submit views depend and retailer it as a customized discipline for every submit. To do that, paste the next codes in your theme’s capabilities.php file or higher in a site-specific plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Now that you’ve this perform in place, we have to name this perform on the only submit pages. This manner the perform is aware of precisely which submit will get the credit score for the views. To do that, you would wish to stick the next code inside your single submit loop:

1
wpb_set_post_views(get_the_ID());

In case you are utilizing a baby theme otherwise you simply wish to make issues simple for your self, then it is best to merely add the tracker in your header through the use of wp_head hook. So paste the next code in your theme’s capabilities.php file or the site-specific plugin:

1
2
3
4
5
6
7
8
9
function wpb_track_post_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;   
    }
    wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');

After getting positioned this, each time a person visits the submit, the customized discipline can be up to date.

Word: In case you are utilizing a caching plugin, this system will NOT work by default. We’re utilizing W3 Complete Cache, and it has the characteristic referred to as Fragmented Caching. You should use that to make this work simply high-quality. Here’s what must be modified:

1
<!-- mfunc wpb_set_post_views($post_id); --><!-- /mfunc -->

Now, you are able to do all form of cool stuff resembling show submit view depend, or kind posts by view depend. Lets check out methods to do a few of these cool issues.

You May Also Like : How to Fix White Text and Missing Buttons in WordPress Visual Editor

If you wish to show the submit view depend in your single submit pages (usually subsequent to the remark depend or one thing). Then the very first thing you might want to do is add the next in your theme’s capabilities.php file or the site-specific plugin.

1
2
3
4
5
6
7
8
9
10
function wpb_get_post_views($postID){
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

Then inside your submit loop add the next code:

1
wpb_get_post_views(get_the_ID());

If you wish to kind the posts by view depend, then you are able to do so simply through the use of the the wp_query post_meta parameter. Essentially the most primary instance loop question would appear to be this:

1
2
3
4
5
6
7
8
<?php
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC'  ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
the_title();
endwhile;
?>

So as to add different WP_Query parameters resembling time vary, consult with the WP_Query web page on Codex.

We hope that you just loved this submit.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

How to Signup for Godaddy Affiliate Program & Start Making Money

GoDaddy is likely one of the well-liked model names out there of area and web-hosting. They are well-liked for a numerous cause, and so they...

How To Start Selling Website on Flippa: A Beginner’s Guide To Success

If you may have been working on-line for lengthy and into Website flipping, Flippa is one identify that you need to have heard of. Flippa is a...

How to Fix Your Connection is Not Private Error (Site Owners Guide)

Are you seeing ‘Your connection is not private’ error in your WordPress web site? This is the error message you’ll see on Google Chrome. The...

How to Fix the WordPress White Page of Death (Step by Step)

The WordPress white page of dying is one of the most widespread WordPress errors. It can also be one of the most irritating ones as...

Recent Comments