July 15, 2025
1 min read

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

functions.php
/*
 ==================
 https://mukto.info
======================	 
*/
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
        $count++;
        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);
functions.php
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
    set_views($post_ID);
}
//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.

your_template_name.php
<div class="post_grid_wrapper">
    <?php
            
            $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>

    </div>
    <?php endwhile; ?>
    <?php endif; ?>

    <?php wp_reset_query(); ?>
</div>

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.

Leave a Reply

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

1 Comment

JetAppointment Set lock time before more than 24 hours

Extend JetAppointment’s “Locked Time Before” booking from 24h to 30 days or more. Developer guide with PHP snippet to customize your booking buffer.

Sequential Fading jQuery Text Animation

This code animates text using jQuery, creating a seamless, captivating effect with elegant fades, enhancing user engagement and enriching web design.

Make Specific Products Not Purchasable or Purchasable for a specific date in WooCommerce

How to make specific products not purchasable in WooCommerce using code.

Protected: WP user Login Notification

There is no excerpt because this is a protected post.

Web Development Project in mind?

if you looking for a web developer for paid contribution to your project I am available for work.

Mukto
Mukto

Click the button below to chat with me.