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.

<?php
function cxc_set_post_views( $postID ) {
	$count_key = 'cxc_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 );
	}
}
?>

Inside the cxc_set_post_views() custom function, we passed the $postID variable which represents the current post id. The cxc_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 cxc_adjacent_posts_rel_link_wp_head is solving the pre-fetching issue. In simple terms it will display post views count properly and accurately.

<?php
remove_action( 'wp_head', 'cxc_adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head', 'cxc_track_post_views');

function cxc_track_post_views( $post_id ) {
	if ( !is_single() ){
		return;
	} 

	if ( empty( $post_id) ) {
		global $post;
		$post_id = $post->ID;    
	}

	cxc_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

<?php
function cxc_get_post_views( $postID ){
	$count_key = 'cxc_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';
}
?>

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

<?php echo cxc_get_post_views( get_the_ID() ); ?>

3. Show Popular Posts by View in WordPress

<?php

$popular_postby_view = array(
   'post_type'=> 'post',
   'orderby'    => 'meta_value_num', // set custom meta key
   'post_status' => 'publish',
   'order'    => 'ASC',
   'meta_key' => 'cxc_post_views_count',
   'posts_per_page' => 4 
);

$popularpost = new WP_Query( $popular_postby_view );

if ( $popularpost-> have_posts() ) : ?>
	<div class="cxc-popular-post-list">
		<?php while ( $popularpost->have_posts() ) : global $post; $popularpost->the_post(); ?>
			<div class="cxc-popular-post">
				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
			</div>
			<?php echo cxc_get_post_views( $post->ID ); 
		endwhile;
	endif; wp_reset_postdata(); ?>
</div>

?>

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

Was this article helpful?
YesNo

Leave a comment

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