1

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 ) : 
    //blahblah
<?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(); ?>">
6
  • 3
    Read following article for general info why you shouldn't use query_posts ever: wordpress.stackexchange.com/questions/1753/… Feb 21, 2013 at 6:42
  • 1
    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, 2013 at 11:36
  • It's WP-Views by Lester Chan wordpress.org/extend/plugins/wp-postviews Feb 21, 2013 at 12:36
  • Yeah I've tried but probably I am missing something. normally when I want to display most viewed posts I use (http://mydomain.com/?v_sortby=views&v_orderby=desc and to use them as a secondary loop I can fetch them using the unrecomendable function query_posts() like query_posts(array('v_sortby' => 'views', 'v_orderby' => 'DESC', 'showposts' => 10, 'cat'=>$cat)); and it works but people say not to use query_posts() so I wanted to try get_posts() or wp_query() but i am having trouble. Feb 21, 2013 at 12:41
  • 1
    Use new WP_Query() for all three loops. After each loop, call wp_reset_postdata(). Feb 21, 2013 at 12:54

1 Answer 1

0

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

    <?php 
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(); ?>

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