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?

  • Try 'orderby' => 'meta_value'.
    – kaiser
    Sep 11, 2012 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? Sep 11, 2012 at 12:17
  • Yea, intercept the posts_clauses filter.
    – kaiser
    Sep 11, 2012 at 12:18
  • Im lost, I cant seem to find much information on the posts_clause filter anywhere :( Sep 11, 2012 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, 2012 at 1:05

1 Answer 1


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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