Title: [Plugin wp-postviews] fetch most viewed post from a category
Last modified: August 19, 2016

---

# [Plugin wp-postviews] fetch most viewed post from a category

 *  Resolved [perfectlover](https://wordpress.org/support/users/perfectlover/)
 * (@perfectlover)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/)
 * Hi,
    Using Gamer’z wp-postviews plugin I am trying to show most viewed post on
   single page. I want to fetch most viewed post from a categories and display on
   that page. Gamerz suggested me to use get_query_var(“cat”) and I am using following
   code on the sidebar. <?php if (function_exists(‘get_most_viewed_category’)): ?
   >
    -  <?php get_most_viewed_category(get_query_var(“cat”),’both’,10); ?>
 * <?php endif; ?>
 * I am just getting N/A and no posts. Since it is not support question, gamerz 
   can not provide any help and is fair enough. I am wondering if some one has tried
   this custom modification.
    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974786)
 * If you are wanting that displayed on a single.php, then aren’t you looking to
   return the most viewed post from the category of the post being displayed on 
   that single page?
 * If so, wouldn’t it be something like:
    <?php $category = get_the_category(); 
   if (function_exists(‘get_most_viewed_category’)) { get_most_viewed_category($
   category[0],’both’,10); } ?> `
 * See:
    [Template_Tags/get_the_category](http://codex.wordpress.org/Template_Tags/get_the_category)
 *  Thread Starter [perfectlover](https://wordpress.org/support/users/perfectlover/)
 * (@perfectlover)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974956)
 * It was solved by Gamerz thanks to him.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974989)
 * Posting a resolution to the problem here would be beneficial to others. Thanks.
 *  [Lester Chan](https://wordpress.org/support/users/gamerz/)
 * (@gamerz)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974990)
 * Happen to see this:
    `<?php if (function_exists('get_most_viewed_category') &&
   intval(get_query_var("cat")) > 0): ?>` `<ul>` `<?php get_most_viewed_category(
   get_query_var("cat"),'both',10); ?>` `</ul>` `<?php endif; ?>` And it works on
   the default theme (archive.php). You cannot put it in anywhere except the archive.
   php because that is the only page where the category information get retrieved.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974991)
 * As always, LC, your support of your plugins is superb! Thanks.
 *  [Lester Chan](https://wordpress.org/support/users/gamerz/)
 * (@gamerz)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/get-query/#post-974999)
 * Hi Michael, Thanks so much for the compliments =D
 *  [kreestal](https://wordpress.org/support/users/kreestal/)
 * (@kreestal)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/get-query/#post-975090)
 * Hello guys,
 * I have the same problem that perfectlover had in the first place. I try to display
   the most viewed posts and the least viewed posts of a category on the category’s
   template (category-N.php).
 * I tried this:
 *     ```
       <?php if (function_exists('get_most_viewed_category') && intval(get_query_var("cat")) > 0): ?>
       <ul>
       <?php get_most_viewed_category(get_query_var("cat"),'both',10); ?>
       </ul>
       <?php endif; ?>
       ```
   
 * but it returns “N/A”.
 * After reading Lester Chan’s forum ([this topic](http://forums.lesterchan.net/index.php/topic,3096.0.html)
   in particular), I came up with this:
 *     ```
       <?php
       query_posts('v_sortby=views&v_orderby=asc');
       $recent_posts = new WP_Query("cat=3&showposts=3&post_status=publish");
       while ($recent_posts->have_posts()):
       $recent_posts->the_post();?>
       <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><span>(<?php the_views() ?> fois)</span></li>
       <?php
       endwhile;
       wp_reset_query();
       ?>
       ```
   
 * Which works fine to display the least viewed posts.
 * However, when I replace “asc” by “desc” in the first query line, I get the exact
   same list, that is to say a list of the LEAST viewed posts, and not the most 
   viewed ones.
 * Any idea why sorting posts by “desc” doesn’t work?
 * The code used to work, though, with 2.7.2. I upgraded to 2.8.2 a few days ago,
   and it seems the problem appeared at that moment.
 * Thanks a lot for your help!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘[Plugin wp-postviews] fetch most viewed post from a category’ is closed
to new replies.

 * 7 replies
 * 4 participants
 * Last reply from: [kreestal](https://wordpress.org/support/users/kreestal/)
 * Last activity: [16 years, 11 months ago](https://wordpress.org/support/topic/get-query/#post-975090)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
