Modify main WordPress loop with a parse_query filter

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.

Read More

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?

Related posts

Leave a Reply

3 comments

  1. 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.. 🙂

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

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