Displaying popular posts on your WordPress site is a great way to showcase your best content and keep visitors engaged. Whether you’re running a blog, a news site, or an e-commerce store, learning how to query popular posts in WordPress can significantly boost user interaction. In this blog post, we’ll explore how to display popular posts in WordPress using both plugins and custom code.
Method 1: Using a Plugin to Display Popular Posts in WordPress
The simplest way to display popular posts in WordPress is by using a plugin. One of the most popular and highly customizable plugins is WordPress Popular Posts.
- Install the plugin:
- Go to your WordPress dashboard.
- Navigate to
Plugins > Add New
. - Search for “WordPress Popular Posts”.
- Click
Install Now
and thenActivate
.
- Configure the plugin to track Post Views in WordPress:
- Go to
Appearance > Widgets
. - Drag the “WordPress Popular Posts” widget to your desired widget area.
- Customize the widget settings to suit your needs, such as the number of posts to display, the time range for popularity, and the criteria for popularity (views or comments).
- Go to
The plugin takes care of tracking views and displaying the popular posts, making it an excellent choice for users who prefer a quick and easy setup.
Method 2: Using Custom Code to Display Popular Posts in WordPress
If you prefer a more hands-on approach or want to avoid using plugins, you can add custom code to your theme’s functions.php
file or a custom plugin. This method gives you more control over how popular posts are tracked and displayed.
Step 1: Track Post Views in WordPress
First, we need to track the number of views for each post. Add the following code to your theme’s functions.php
file:
// Function to count post views.
function set_post_views($postID) {
$count_key = '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);
}
}
// Function to get post views.
function get_post_views($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';
}
// Hook into 'wp_head' to track views.
function track_post_views ($post_id) {
if ( !is_single() ) return;
if ( empty ( $post_id) ) {
global $post;
$post_id = $post->ID;
}
set_post_views($post_id);
}
add_action( 'wp_head', 'track_post_views');
This code defines functions to set and get post views and hooks into the wp_head
action to increment the view count whenever a post is viewed.
Step 2: Create a Custom Query to Display Popular Posts in WordPress
Next, create a custom query to display the most popular posts based on view count. Add the following code to your theme’s template file where you want to display the popular posts:
// Query to get popular posts.
$popular_posts_args = array(
'posts_per_page' => 5, // Number of popular posts to display.
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$popular_posts_query = new WP_Query( $popular_posts_args );
// Loop through the posts.
if ( $popular_posts_query->have_posts() ) :
while ( $popular_posts_query->have_posts() ) : $popular_posts_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php echo get_post_views(get_the_ID()); ?></p>
<?php endwhile;
wp_reset_postdata();
endif;
This code creates a custom query to retrieve the most popular posts and displays them in a loop.
Method 3: Using WP_Query for Comments to Highlight Popular Posts in WordPress
If you want to base the popularity on the number of comments, you can modify the query as follows:
$popular_posts_args = array(
'posts_per_page' => 5, // Number of popular posts to display.
'orderby' => 'comment_count',
'order' => 'DESC',
);
$popular_posts_query = new WP_Query( $popular_posts_args );
// Loop through the posts.
if ( $popular_posts_query->have_posts() ) :
while ( $popular_posts_query->have_posts() ) : $popular_posts_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php comments_number(); ?></p>
<?php endwhile;
wp_reset_postdata();
endif;
This query orders the posts by the number of comments in descending order, allowing you to display posts that are popular based on engagement.
Conclusion
Displaying popular posts on your WordPress site can significantly enhance user engagement by showcasing your best content. Whether you choose to use a plugin for a quick setup or implement custom code for more control, you can easily highlight popular posts based on views or comments. Try these methods on your site and see how they boost interaction and keep visitors coming back for more!