How to Display Most Popular Posts from a Specific Category

There are many way to make visitor browsing more pages in your WordPress Blog. One of them is showing most popular post on your sidebar or footer. This also help your visitors finding your best content. Most popular here I mean, most commented posts

How to display most popular post from specific category

There tons article out there to display the popular posts, one of them is our article : Display your Most Popular Posts with Thumbnails

Display Most Popular Post from Specific Category

But, what if the popular posts We want to display are from a single category only? This is very useful if We have a category We want to promote, by displaying its popular posts

Ok, I’ll share the solution here. Put the code below, in your theme sidebar or wherever You want to display your popular posts from specific category



<?php
$args=array(
  'cat' => 3,
  'orderby' => 'comment_count',
  'order' => 'DESC',
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 6,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
	<ul>
	<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
	<?php  endwhile; ?>
	</ul>
<?php }

wp_reset_query(); ?>


Explanation

'cat' => 3,

replace 3 with category ID you want to display

'posts_per_page' => 6,

This is to set how much post You want to display. Here, 6 posts.

Hope this tutorial helps

Credit:
Thanks to mkjar and MichaelH from WordPress.org

8 Comments to “How to Display Most Popular Posts from a Specific Category”

Add Comments (+)

  1. kumbi says:

    great post! great snippet! can i generate this list based on the number of page views without resorting to a plugin?

  2. Matteo says:

    Great snippet, but i have a question: is it possibile to limit popular post per category selecting only last 7 or 30 days?

    Thank you in advance :)

  3. nivin says:

    it is nice

  4. Pieter says:

    Too bad it doesn’t work on a custom post type single page…

  5. Muzza says:

    Nice! One question: how would you go about category ID relevant to the page you’re on?

    Example;

    I want to pull out the most popular post (14 days) per category on the category.php pages…

    Thoughts?

  6. Diogo92 says:

    Thank you!!! You saved my life :D

  7. lowestofthekeys says:

    Awesome code man, you’re lifesaver. I scoured the web for hours trying to find a decent plugin to do the same thing, fortunately I decided to change what I was looking for and found this. 
     
    Also, works great with CSS.

  8. Abe says:

    I’ve try but It doesn’t work for my site, I’ve try to add this script to a page that shown my popular post but the result is not displaying post by most views

Trackbacks/Pingbacks

  1. How to Order Categories by Most Recently Updated
  2. How to Add New Class to First Post in The Homepage
  3. Shun the Plugin: 100 WordPress Code Snippets from Across the Net | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org
  4. 55 Best WordPress Tricks And Hacks
  5. Useful WordPress Code Snippets and Hacks | webexpedition18
  6. 30 Extremely Useful Wordpress Code Snippets & Hacks | Web Design Habits
  7. 100 WordPress Code Snippets from Across the Net « Tech Snippets
  8. 15 WordPress Code Snippets/Hacks to Use [WP Development] | gonzoblog

Leave a Reply

 

Amazingly Beautiful WordPress Themes