I have a site and i'm almost done with it. It have teached me a lot about wordpress, although i'm still a noob who doesn't know anything about php or programming (besides html).

I have managed to create a loop as a page template. But I want to show posts ordered by most viewed and most rated.

My loop looks like this

Template Name: Most watched

query_posts( 'posts_per_page=5' );

 <!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <!-- The following tests if the current post is in category 3. -->
 <!-- If it is, the div box is given the CSS class "post-cat-three". -->
 <!-- Otherwise, the div box will be given the CSS class "post". -->
 <?php if ( in_category('3') ) { ?>
           <div class="post-cat-three">
 <?php } else { ?>
           <div class="post">
 <?php } ?>

 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

 <!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>

 <!-- Display the Post's Content in a div box. -->
 <div class="entry">
   <?php the_content(); ?>

 <!-- Display a comma separated list of the Post's Categories. -->
 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
 </div> <!-- closes the first div box -->

 <!-- Stop The Loop (but note the "else:" - see next line). -->
 <?php endwhile; else: ?>

 <!-- The very first "if" tested to see if there were any Posts to -->
 <!-- display.  This "else" part tells what do if there weren't any. -->
 <p>Sorry, no posts matched your criteria.</p>

 <!-- REALLY stop The Loop. -->
 <?php endif; ?>

Really simple - taken form the wordpress documentation examples. I'm using the two plugins wp-postviews and wp-postratings.

How should my loop look, if I want to order posts using those plugins? I googled it a lot but never found a complete example - only code snips that i don't know where to put in :(

Thanks in advance!

  • Thank's For Your Coding Help. It's Helpful For Me. My issue has <?php $pc = new WP_Query('orderby=views_count&posts_per_page=3&order=DESC'); ?> Replace It. <?php $pc = new WP_Query('posts_per_page=3&v_sortby=views&v_orderby=asc'); ?> Now working. – user19267 Aug 15 '12 at 5:04

Having taken a quick look at the wp-postviews documentation, it seems you can change query_posts( 'posts_per_page=5' ); to query_posts( 'posts_per_page=5&v_sortby=views&v_orderby=desc' );, but I haven't tested that. Give it a shot and see how it turns out.

A cautionary note on doing this: What often happens when you sort by views is that the first page will NEVER change. Transient traffic will look at the first couple, boosting their numbers, but not click through. I suggest that you make other sort options available to your users, should they want them.

  • Wow. Thank you my good man - you did the job. It actually works know. I know what you mean - and if I knew how to make some kind of order-function, I would. But for know - this will have to do :P Thanks a lot! – Torben Feb 12 '12 at 16:10
  • Don't forget to mark the question solved so that anyone who comes by in future can benefit from your learning. – mor7ifer Feb 12 '12 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.