Most View or Popular Post on WordPress

WordPress Post query by user views without plugin and simple code snippet

With this code, we will show posts base on user views. The main trick is we will put a counter that will count how many time that page get views.

First Step

Put This code on functions.php

function set_views($post_ID) {
    $key = 'views';
    $count = get_post_meta($post_ID, $key, true); //retrieves the count

    if($count == ''){ //check if the post has ever been seen

        //set count to 0
        $count = 0;

        //just in case
        delete_post_meta($post_ID, $key);

        //set number of views to zero
        add_post_meta($post_ID, $key, '0');

    } else{ //increment number of views
        update_post_meta($post_ID, $key, $count);

//keeps the count accurate by removing prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
function track custom_post_watch ($post_ID) {
    //you can use is_single here, to track all your posts. 
   //Here, we're traking custom post 'place'
    if ( !is_singular( 'place') ) return; 

    if ( empty ( $post_ID) ) {

        //gets the global post
        global $post; 

        //extracts the ID
        $post_ID = $post->ID;    

    //calls our previously defined methos
//adds the tracker to wp_head.
add_action( 'wp_head', 'track_custom_post_watch');

Now we have a counter set up with the key, let show post on index.php or any page template as you want.

<div class="post_grid_wrapper">
            $args = array(
                'post_type'     => 'place', //your post type
                'posts_per_page' => 5, // how many post will show
                'meta_key'      => 'views', //the metakey previously defined
                'orderby'       => 'meta_value_num',
                'order'         => 'DESC'
            $query = new WP_Query( $args );

    <?php if( $query->have_posts() ) : ?>
    <?php $ppcount = 1;?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

    <div class="post_item">
        <?php  the_post_thumbnail()?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

    <?php endwhile; ?>
    <?php endif; ?>

    <?php wp_reset_query(); ?>

It’s done! Is not it so simple? 😛 You will need to customize some HTML to show posts as you want. If post not showing on the template page, then visit some posts so view value gets some count.

Previous Code

wp-config development code

WordPress post revisions, debug, load-scripts.php ...

Next Code

WordPress category or taxonomy list

WordPress custom taxonomy term list with function ...

1 Comment

Leave a Reply

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

10 − 5 =

WordPress Ajax Search without plugin [ Easy ]

WordPress Ajax Search without plugin. Fully custom ...

Remove Website field from WordPress comment & Change cookies remember text

Remove Website field from WordPress comment & Chan ...

WordPress Next and Previous Post

WordPress Next and Previous Post navigation for cu ...

Conditional statement to show pagination

Conditional statement to show pagination on WordPr ...

WooCommerce Mostly used Shortcode

Most use shortcode for popular eCommerce plugin Wo ...









Need Coding Help?

Connect Skype