Support » Fixing WordPress » wp_query orderby not working in custom loop

  • Resolved Roy Tanck

    (@roytanck)


    I’m building a theme for a WPMU 2.7.1 hosted blog. The client wants an archive of all posts on every single post page, which is why I’m using wp_query to create a second loop. My problem is that the ‘orderby’ parameter seems not to work propery.

    I use an $order var that can be either ‘date’, ‘author’, ‘rand’ or ‘title’. (This code also includes a fix I found that restores the old query that may not be needed here.)

    <?php
    	$temp = $wp_query;
    	$wp_query= null;
    	$wp_query = new WP_Query();
    	$ascdesc = ( $order == 'title' || $order == 'author' ) ? 'ASC' : 'DESC';
    	$queryStr = 'showposts=-1&orderby='.$order.'&order='.$ascdesc;
    	$wp_query->query($queryStr);
    ?>
    
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <!-- loop stuff -->
    <?php endwhile;?>
    <?php $wp_query = null; $wp_query = $temp;?>

    Om my test install of WPMU 2.3.3 (which I need to keep at that old version to match another client install) it works OK, except for ‘rand’ which orders by date.

    On the 2.7.1 install, no matter which order I select, posts are always ordered by date.

    Does anyone have any idea what could be wrong here?

Viewing 5 replies - 1 through 5 (of 5 total)
  • I don’t see where $order is ever set.

    Add this just before the query to check the value of $queryStr

    echo "<pre>"; print_r($queryStr); echo "</pre>";

    Also note that the MU forums are at: http://mu.wordpress.org/forums

    Thread Starter Roy Tanck

    (@roytanck)

    Hi MichaelH. I did that, and the query looks 100% OK to me. This is from the client’s blog:

    showposts=-1&orderby=rand&order=DESC

    I wasn’t (and still am not) sure this is an MU issue. Don’t have a regular WP version running anywhere of the same version, but will try 2.8.4 to see if the theme works correctly there.

    EDIT: all sort options work under regular WP 2.8.4

    Okay. Marking this resolved.

    Thread Starter Roy Tanck

    (@roytanck)

    Thanks. I’ll post over at the MU forums to see if there are any known issues with ‘orderby’ under 2.3.3 and 2.7.1. I need to target 2.7 for now.

    Hello everybody,

    I’ve just set up 2.9.1 and it seems that orderby does not work. Here is my code :

    $my_query = new WP_Query('showposts=2&cat=33&orderby=meta_value&meta_key=Date_Tri&order=desc');
    
    echo $my_query->query;
    $wpdb->show_errors()."<br />";
    
    while ($my_query->have_posts()) {
    	$my_query->the_post();
    	echo "<h2><b><u>";
    	echo get_post_meta($post->ID, 'Date_Tri', true).the_title();
    	echo "</u></b></h2>";
    	the_content();
    }

    Date_tri is some kind of text date ( 2009-02-15 for instance)

    which gives :

    showposts=2&cat=33&orderby=meta_value&meta_key=Date_Tri&order=desc

    testt2009-01-01
    Post one text ….

    Los Van Van au Bataclan le 2 mars 2010
    2009-01-02
    Post two text ….

    Have I missed anything ?

    Thank you for your help

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_query orderby not working in custom loop’ is closed to new replies.