How to Display Most Recent Posts, Most Popular Posts, Recommended Posts in WordPress

This tutorial will teach you how to display your Most Recent Posts, Most Popular Posts, and Recommended Posts in your WordPress-powered website.

How to Display Most Recent Posts

Add the following lines of code where you want to display your most recent posts:

<div class="posts-most-recent widget">
<h4 class="widget-title">Most Recent Posts</h4>
<ul>
	<?php 
	$theme_recent_posts = new WP_Query('showposts=5');
	if ( $theme_recent_posts->have_posts() ) : while ($theme_recent_posts->have_posts()) : $theme_recent_posts->the_post(); 
	?>
		<li>
			<a href="<?php esc_url(the_permalink()); ?>"><?php esc_html(the_title()); ?></a>
		</li>
	<?php endwhile; ?>
	<?php endif; ?>
	<?php wp_reset_postdata(); ?>
</ul>
</div>

How to Display Most Popular Posts

1. Add the bits of code below to the functions.php file:

// Display Most Popular Posts
function getPostViews($postID){
$count_key = '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';
}
function setPostViews($postID) {
$countKey = 'post_views_count';
$count = get_post_meta($postID, $countKey, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $countKey);
add_post_meta($postID, $countKey, '0');
}else{
$count++;
update_post_meta($postID, $countKey, $count);
}
}

2. Add the following where you want to display the most popular posts:

<div class="posts-most-popular widget">
<h4 class="widget-title">Most Popular Posts</h4>
<ul>
	<?php 
	$theme_popular_posts = new WP_Query('meta_key=post_views_count&orderby=meta_value_num&order=DESC&posts_per_page=5');
	if ( $theme_popular_posts->have_posts() ) : while ($theme_popular_posts->have_posts()) : $theme_popular_posts->the_post(); 
	?>
		<li>
			<a href="<?php esc_url(the_permalink()); ?>"><?php esc_html(the_title()); ?></a>
		</li>
	<?php endwhile; ?>
	<?php endif; ?>
	<?php wp_reset_postdata(); ?>
</ul>
</div>

3. Insert this line into the single.php loop:

<?php setPostViews(get_the_ID()); ?>

How to Display Recommended Posts

1. Add the following lines of code where you want to display your recommended posts:

<div class="posts-recommended widget">
<h4 class="widget-title">Recommended Posts</h4>
<ul>
	<?php 
	$theme_featured_posts = new WP_Query('posts_per_page=5&category_name=Recommended');
	if ( $theme_featured_posts->have_posts() ) : while ($theme_featured_posts->have_posts()) : $theme_featured_posts->the_post(); 
	?>
		<li>
			<a href="<?php esc_url(the_permalink()); ?>"><?php esc_html(the_title()); ?></a>
		</li>
	<?php endwhile; ?>
	<?php endif; ?>
	<?php wp_reset_postdata(); ?>
</ul>
</div>

2. Go to Posts > Categories in the WordPress dashboard and create a new category. Write Recommended as the name, recommended as the slug, choose none for the Category Parent, and click Add New Category.

3. Go to Posts > Add New in the WordPress dashboard and create a new post. Tick the checkbox next to the category Recommended in the Categories box on the right. Click Publish.

4. Repeat Step 3 as many times as you want.