How to Display Most Popular Posts from a Specific Category
26th Jan 2011 | Posted by reza | 16 CommentsThere 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
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 (+)-
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
-
it is nice
-
Too bad it doesn’t work on a custom post type single page…
-
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?
-
Thank you!!! You saved my life
-
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. -
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
- How to Order Categories by Most Recently Updated
- How to Add New Class to First Post in The Homepage
- Shun the Plugin: 100 WordPress Code Snippets from Across the Net | WordPress, Multisite and BuddyPress plugins, themes, news and help – WPMU.org
- 55 Best WordPress Tricks And Hacks
- Useful WordPress Code Snippets and Hacks | webexpedition18
- 30 Extremely Useful Wordpress Code Snippets & Hacks | Web Design Habits
- 100 WordPress Code Snippets from Across the Net « Tech Snippets
- 15 WordPress Code Snippets/Hacks to Use [WP Development] | gonzoblog
great post! great snippet! can i generate this list based on the number of page views without resorting to a plugin?