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

I have the following loop used to display posts by category & I would like to to re-order them based on a dropdown selector

if (!isset($qry))
    $qry = new WP_Query(array('category_name'=>$GLOBALS['city'],'order'=>$orderby));    

if ($qry->have_posts($orderby)) {
    $do_the_post = null;

    if (is_single($orderby)) {
        $do_the_post = true;
    } else {
        $do_the_post = false;

    $user = get_userdata($post->post_author);

So far, using some code I found here

I have:

if( isset($_GET['o']) && $_GET['o'] != '')
    $order = $_GET['o'];
        case 'date-asc': $orderby = 'order=ASC';
            $msg = 'Date Ascending';
        case 'date-desc': $orderby = 'order=DESC';
        $msg = 'Date Descending(default)';
        case 'date-mod': $orderby = 'orderby=modified';
            $msg = 'Date Modified';
        case 'title-asc': $orderby = 'orderby=title&order=ASC';
            $msg = 'Title A-Z';
        case 'title-desc': $orderby = 'orderby=title&order=DESC';
            $msg = 'Title Z-A';
        case 'comment': $orderby = 'orderby=comment_count';
            $msg = 'Comment Count';
        $orderby = 'order=DESC';
        $msg = 'Date Descending (default)';

            <div id="sorter-container">
            <select id="order-by">
                <option value="date-desc" <?php echo (!isset($order) || $order == '' || $order == 'date-desc')? 'selected="selected"':''; ?>>Date Desc. (default)</option>
                <option value="date-asc" <?php echo ($order == 'date-asc')? 'selected="selected"':''; ?>>Date Asc</option>
                <option value="date-mod" <?php echo ($order == 'date-mod')? 'selected="selected"':''; ?>>Date Modified</option>
                <option value="title-desc" <?php echo ($order == 'title-desc')? 'selected="selected"':''; ?>>Title Desc.</option>
                <option value="title-asc" <?php echo ($order == 'title-asc')? 'selected="selected"':''; ?>>Title Asc.</option>
                <option value="comment" <?php echo ($order == 'comment')? 'selected="selected"':''; ?>>Comments Count</option>


    <script type="text/javascript">
        var orderby = jQuery('#order-by');
        var str;
        str = jQuery(this).val();
        window.location.href = "<?php echo home_url(); ?>/gallery/?o="+str;

Is calling $orderby as the order is not working - my PHP is rudimentary at best, but can anyone tell me what's wrong here?


share|improve this question
up vote 0 down vote accepted

I guess the problem is

$qry = new WP_Query(array('category_name'=>$GLOBALS['city'],'order'=>$orderby)); 

Let's just say $orderby is orderby=title&order=ASC

Then the order argument of WP_Query will be like this:


However, order argument only accepts: ASC and DESC.

You should do somethings like this:

share|improve this answer
This is great & works, thankyou. One quick question if you don't mind.... Taking the array out won't make a difference? – Christopher Camplin Mar 1 '13 at 13:22
it'll be automatically converted to array by wp_parse_args. yep, WP_Query will use wp_parse_args – Jesse Mar 1 '13 at 13:29
big thanks for your help. – Christopher Camplin Mar 1 '13 at 15:11

Your Answer


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.