Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

I'm using WordPress 3.0.4.

share|improve this question
add comment

3 Answers 3

Try with this:

$args=array(
 'posts_per_page'      => 15, 
 'post_type'     => 'post', 
 'key' => 'views',
 'orderby' => 'meta_value_num', 
 'order'    => 'ASC',
 'post_status' => 'publish'
);

query_posts($args); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
share|improve this answer
add comment
up vote 3 down vote accepted

Just wanted to give an update. Someone was kind enough to show me how to update the code I posted above. It turned out to be quite simple (if you know what you're doing!). So I have posted it below for anyone that wants to do something similar.

<?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' => 'DESC',
); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post() ;?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
share|improve this answer
add comment

From the plugin documentation

To Display Most Viewed Posts Use:

<?php if
(function_exists('get_most_viewed')):
?>
<ul>
      <?php get_most_viewed(); ?>    
</ul> <?php endif; ?> 

The first value you pass in is what you want to get, 'post', 'page' or 'both'. The second value you pass in is the maximum number of post you want to get.

Default: get_most_viewed('both', 10);

share|improve this answer
    
Thanks for replying. I can see how to get a list of the posts, what I'm struggling with though is how to change the loop I've posted above to get the posts in order of most viewed. –  PG73 Jan 12 '11 at 12:44
    
You'll need to add a custom sorting to the order_by parameter then. Have a look at this: gab.ro/post-sorting-reloaded which is a plugin that 'wraps the posts_orderby filter' for some ideas. However, it looks like posts_orderby has been deprecated - so you might need to be careful there –  anu Jan 12 '11 at 13:19
    
Thanks, I will take a look at that. I was hoping though that someone on here might have done something similar in the past and could show me exactly how my code above would need to be changed. Though from what you've said it doesn't sound like a simple task. –  PG73 Jan 12 '11 at 16:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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