change home page sorting (3 posts)

  1. gswartz
    Posted 3 years ago #

    For our home page I've been asked to change the ordering from most recent to order by most views. I have no idea how to do this. Can anyone point me in the right direction.

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    WordPress doesn't store Post or Page view, so the first thing you'll need to do is find a plugin that will store this data for you.

  3. gswartz
    Posted 3 years ago #

    I found this - http://wordpress.org/support/topic/change-loop-to-order-posts-by-views and it "seems" to work but not quite. I am getting posts with the larger views at the top but then I have posts with smaller views interspersed. You can see how it's working here - http://support.topechelon.com And here's the code I'm using. Notice there's a section that shows the number of views in the code. It's calling a function that returns get_post_meta($pid,'post_viewed_count',true) which is what the args also order by, so I would think my number of views would decrease per row instead of going from 514 to 154 to 40 to 185.

    <?php $paged = intval(get_query_var('paged')); ?>
    <?php $paged = ($paged) ? $paged : 1; ?>
    $args = array(
       'orderby' => 'post_viewed_count',
       'order' => 'ASC',
    <?php query_posts($args); ?>
    <!-- ****** END ORDER BY VIEWS STUFF -->
    <?php while ( have_posts() ) : the_post(); ?>
              /* Include the Post-Format-specific template for the content.
               * If you want to overload this in a child theme then include a file
               * called content-___.php (where ___ is the Post Format name) and that will be used instead.
               */ ?>
              <div class="entry">
    		  <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    		  <div class="views"><?php echo user_post_visit_count($post->ID);?> Views</div><div class="categories"><?php the_category(', '); ?></div>
      <?php endwhile; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic