• Resolved pg73

    (@pg73)


    Hi, I hope somebody would be kind enough to help. I currently have a page that orders all of the posts by comment count, but I want to change the loop to order the posts by view count. I have installed the ‘wp-postviews.1.50’ plugin and have got it to display the number of views on each post, so I know that side of it is working, now I just need the loop code changed to order by most views, is this possible?

    This is the how the number of views is called out:

    <?php $views = get_post_meta($post->ID, 'views', true); ?><?php echo $views; ?>

    And here is the loop I need changed:

    <?php $posts_per_page = get_query_var('posts_per_page'); ?>
    <?php $paged = intval(get_query_var('paged')); ?>
    <?php $paged = ($paged) ? $paged : 1; ?>
    <?php $args = array(
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'more' => $more = 0,
    'orderby' => 'comment_count',
    'order' => 'DESC',
    ); ?>
    <?php query_posts($args); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post() ;?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

    Thanks in advance for any help with this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think this will do what you want:

    <?php $posts_per_page = get_query_var('posts_per_page'); ?>
    <?php $paged = intval(get_query_var('paged')); ?>
    <?php $paged = ($paged) ? $paged : 1; ?>
    <?php $args = array(
       'posts_per_page' => $posts_per_page,
       'paged' => $paged,
       'more' => $more = 0,
       'meta_key' => 'views',
       'orderby' => 'meta_value_num',
       'order' => 'ASC',
    ); ?>
    <?php query_posts($args); ?>
    Thread Starter pg73

    (@pg73)

    Thank you vtxyzzy, that worked like a charm! I’ve been searching everywhere to see how to do this and couldn’t get an answer and it turned out to be quite simple (if you know what you’re doing!). I really appreciate your help, thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to Change Loop to Order Posts by Views’ is closed to new replies.