WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have a template which initially had one single loop and it was using query_posts, I added 2 more loops using the same method, However after reading allot about these it was concluded that I shall stop using it and everyone is against using it.

original query_posts loop (latest posts)

<?php if ( !is_front_page() && tfuse_options(PREFIX.'_pagination_portfolio') ){
    $cat = get_query_var( 'cat' );
    query_posts( array ( 'posts_per_page' => -1, 'cat'=>$cat ) );
while (have_posts()) : the_post(); ?>
    //blah posts here
<?php endwhile; ?>

2nd custom get_posts loop (random posts)

<?php if ( !is_front_page() ){
    //$cat = get_query_var( 'cat' );
    $args = array( 'numberposts' => 10, 'orderby' => 'rand' );
    $rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
    //blah posts here
<?php endforeach; ?>

3rd custom get_posts loop no working (most viewed posts)

if ( !is_front_page() ){
    $cat = get_query_var( 'cat' );
    $args = array( 'v_sortby' => 'views', 'v_orderby' => 'DESC', 'numberposts' => 10, 'cat'=> $cat);
    $most_viewed_posts = get_posts( $args );
foreach( $most_viewed_posts as $post ) : 
<?php endforeach; ?>

The problem is that the third loop doesnt show the right posts, as if it doesnt recognize the post meta, however if i use query_posts then it works!

views v_sortby, v_orderby

Any help is appreciated, I am quite new to this.

Update According to similar question asked on this site. below is another way of achieving it using query_posts with meta keys. I guess there is a better way of rewriting the code to use meta keys + wp_query or get_posts someone care to? I am very new to this, looks too chunky? :S

<?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 question

marked as duplicate by Chip Bennett, s_ha_dum, kaiser, brasofilo, toscho Feb 21 '13 at 16:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Read following article for general info why you shouldn't use query_posts ever: wordpress.stackexchange.com/questions/1753/… – Eugene Manuilov Feb 21 '13 at 6:42
Already did, thanks – Jose David Garcia Llanos Feb 21 '13 at 6:50
what plugin is used for the 'most viewed'? have you tried to re-write the loops or at least the third loop with WP_Query()? – Michael Feb 21 '13 at 11:36
It's WP-Views by Lester Chan wordpress.org/extend/plugins/wp-postviews – Jose David Garcia Llanos Feb 21 '13 at 12:36
Use new WP_Query() for all three loops. After each loop, call wp_reset_postdata(). – Chip Bennett Feb 21 '13 at 12:54
up vote 0 down vote accepted

Thanks to tips given by the commentators I was able to achieve this :)

if ( is_front_age() ){
                    $most_viewed_posts = new WP_Query( array('v_sortby' => 'views', 'v_orderby' => 'DESC', 'showposts' => 10, 'cat'=> 3) );
                    while ( $most_viewed_posts->have_posts() ) : $most_viewed_posts->the_post(); ?>

//post structure

 <?php wp_reset_postdata(); ?>
share|improve this answer

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