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 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 {
        $qry->the_post($orderby);
        $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'];
    switch($order)
    {
        case 'date-asc': $orderby = 'order=ASC';
            $msg = 'Date Ascending';
            break;
        case 'date-desc': $orderby = 'order=DESC';
        $msg = 'Date Descending(default)';
            break;
        case 'date-mod': $orderby = 'orderby=modified';
            $msg = 'Date Modified';
            break;
        case 'title-asc': $orderby = 'orderby=title&order=ASC';
            $msg = 'Title A-Z';
            break;
        case 'title-desc': $orderby = 'orderby=title&order=DESC';
            $msg = 'Title Z-A';
            break;
        case 'comment': $orderby = 'orderby=comment_count';
            $msg = 'Comment Count';
            break;
    }
    }
    else
    {
        $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>
        </select>
        </div>

//JQUERY

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

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

Thanks...

share|improve this question

1 Answer 1

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:

'order'=>'orderby=title&order=ASC'

However, order argument only accepts: ASC and DESC.

You should do somethings like this:

WP_Query('category_name='.$GLOBALS['city'].'&'.$orderby);
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

 
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.