Forums

[resolved] [Plugin wp-postviews] fetch most viewed post from a category (8 posts)

  1. perfectlover
    Member
    Posted 3 years ago #

    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.

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    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

  3. perfectlover
    Member
    Posted 3 years ago #

    It was solved by Gamerz thanks to him.

  4. MichaelH
    Volunteer
    Posted 3 years ago #

    Posting a resolution to the problem here would be beneficial to others. Thanks.

  5. Lester Chan
    Member
    Posted 3 years ago #

    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.

  6. MichaelH
    Volunteer
    Posted 3 years ago #

    As always, LC, your support of your plugins is superb! Thanks.

  7. Lester Chan
    Member
    Posted 3 years ago #

    Hi Michael, Thanks so much for the compliments =D

  8. kReEsTaL
    Member
    Posted 2 years ago #

    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 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!

Topic Closed

This topic has been closed to new replies.

About this Topic