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 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

3 Answers 3

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

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 ) )
    continue;

There's a ticket here for this which outlines what we should be able to do.
http://core.trac.wordpress.org/ticket/16735

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.

http://scribu.net/wordpress/advanced-metadata-queries.html

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

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

 
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.