Ultimately all meta queries get run through _get_meta_sql
.
-
Line 402 – 403 in the
_get_meta_sql
function
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 areasc
ordesc
(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.. 🙂