WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

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 am trying to customise a search query so that items returned are ordered in descending order. I have managed to find the loop that generates the results....

global $wp_query;
$args = array(
    'post_status' => 'publish',
    'post_type' => 'listings',
    'meta_key' => '_ct_price',
    'orderby' => '_ct_price',
    'order' => 'DESC'

// save the existing query
$existing_query_obj = $wp_query;

$wp_query = new WP_Query( $args ); 
//$wp_query = new WP_Query( $search_values ); 


/** Prepare the title string by looping through all
the values we're going to query and put them together */
$search_params = ''; 
$loop = 0;
foreach ($search_values as $t => $s) {                                     
    if ($loop == 1) { 
        $search_params .= ', '; 
    } else {
        $loop = 1;

    $term = get_term_by('slug',$s,$t);
    $name = $term->name;   
    $search_params .= '<strong>'. $name .'</strong>';                                    

As you can see I have added in an $args section with the ordering information in, it doesnt work currently, but if I comment out the following line..

$wp_query = new WP_Query( $args ); 

And uncomment the following line....

$wp_query = new WP_Query( $search_values );

Then it does work but ignores the rest of the code, is there a way to add my $args onto the exiting query without breaking it?

share|improve this question
Try 'orderby' => 'meta_value'. – kaiser Sep 11 '12 at 12:04
That works for me, but if I uncomment out the $wp_query = new WP_Query( $search_values ); line things still break. Is there a way to include the $args with the $seach_values? – fightstarr20 Sep 11 '12 at 12:17
Yea, intercept the posts_clauses filter. – kaiser Sep 11 '12 at 12:18
Im lost, I cant seem to find much information on the posts_clause filter anywhere :( – fightstarr20 Sep 11 '12 at 18:13
Just search the site via Google. I know that there're tons of good answers about it. Some of them are from me, so I know they exist ;) – kaiser Sep 12 '12 at 1:05

You'll want 'orderby' => 'meta_value' to sort by the specified meta_key or 'orderby' => 'meta_value_num' if it's a numeric value.

share|improve this answer

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.