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 modify the main WordPress loop on my category pages like so:

add_filter('parse_query', 'my_modified_query');

function my_modified_query( $q ) {
       if (!is_admin() && is_category()) {
              $q->set( 'orderby', 'meta_value' );
              $q->set( 'meta_key', 'my_key' );

       return $q;

but it does not seem to work.

However modifying anything else like :

 $q->set( 'order','title' );
 $q->set( 'orderby', 'DESC');

works perfectly. Just not

$q->set( 'orderby', 'meta_value' );
$q->set( 'meta_key', 'my_key' );

any idea why that is?

share|improve this question
Why are you using parse_query instead of the pre_get_posts filter? – Tom J Nowell Jun 1 '15 at 20:11

Ultimately all meta queries get run through _get_meta_sql.

The following coditional statement runs for any meta queries.

if ( empty( $meta_key ) && empty( $meta_value ) )

There's a ticket here for this which outlines what we should be able to do.

What you can do however is purposely pass your query a meta_value your posts will never likely have and use the != (not equals) comparison, technically that should get you the right posts, eg..

$q->set( 'meta_key', 'my_key' );
$q->set( 'meta_value', 'WPSE_RULES!' );
$q->set( 'meta_compare', '!=' );
$q->set( 'orderby', 'meta_value' );

There's one further thing though, you've got these the wrong way round before..

$q->set( 'order','title' );
$q->set( 'orderby', 'DESC');
  • order sets the which direction to order the results by, valid values are asc or desc (upper or lowercase).

  • orderby sets what to the order the results by, eg. date, title, meta value, etc..

I didn't mention that there's actually a new method for querying posts based on meta now, using the meta_query parameter, but seeing as that won't avoid the problem i've mentioned above and older meta parameters still(in the end) get converted into a meta_query anyway, i'll simply offer up a link to some examples.


Hope that all helps.. :)

share|improve this answer
I got 2 replies! That really made my day (I usually ask things and never get replies, he, he). I'll definetely try what you suggest. Thanks for that. I know about the order and ordeby switch, that was accidental but thanks for pointing that out too :) – Sarah Neuber Mar 30 '11 at 7:48
Note, you can(or should be able to) use set_query_var inside parse_query filters, which basicailly does the same as $q->set is just looks prettier... :) – t31os Mar 30 '11 at 10:01
Ok, so I tried everything suggested but nothing seems to sort my posts with the specifc custom field but I can say that I managed to make it work using the add_filter('request', 'my_modified_query'); Still cannot understand why the same function would not work on the add_filter('parse_query', 'my_modified_query'); which is a shame as I loose the ability to use conditional tags :( – Sarah Neuber Mar 31 '11 at 11:46
Just before the end of the parse_query filter add something like update_option( 'problem_vars', $q->query_vars ); for one page load, then print it out somewhere.. print '<pre>';print_r( get_option( 'problem_vars' ) );print '</pre>'; and check all the query vars are as they should be.. – t31os Mar 31 '11 at 17:26
@ t31os, Thanks for this! Well, still nothing :( I can see that my variables are filled correctly: [orderby] => meta_value and [meta_key] => price but no results whatsoever (posts in that category have the "price" custom field set so I should get some results!) This can't be a bug can't it? Especially when the filtering works on the request filter. – Sarah Neuber Apr 1 '11 at 10:05

you probably got the solution as it passed almost an year since you have posted. I had the same issue few hours ago until I have figured it out that I was using numbers - so I used meta_value_num insted of meta_value.

share|improve this answer

Servus Sarah. I guess this is for one of your envato-shop themes? Have you already tried to print the query like this (before return): echo '<pre>'; print_r($q); echo '</pre>';.

I guess you're adding this inside a themes functions.php?

$q->set( 'oderby', 'DESC' ); works, but $q->set( 'oderby', 'meta_value' ); not? Try using two basic orderby parameters separated by spaces to check if the default behavior works all over. If yes, you might just run into some misspelling. Do other values aside from 'my_key' work for 'meta_key'? If not, then maybe you're missing a 'meta_value' parameter. Not sure on this one, but take a look at this one: http://codex.wordpress.org/Function_Reference/WP_Query#Parameters or even better a direct look at the function inside the core file.

share|improve this answer
Servus kaiser, seems you know me :) Wish I knew who you are too! I triple checked the spelling but it all seems good. It bugs me that I cannot find what it is. I'll check again though. Thanks for the reply! – Sarah Neuber Mar 30 '11 at 7:46
Danke! Schon gemacht! :) – Sarah Neuber Mar 31 '11 at 15:17

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.