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 want to order my posts by a custom meta field "fieldA". I am using the following code:

$query->set( 'meta_key', 'fieldA);
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );

Ordering works fine but there is a problem. All posts which do not have the meta_key "fieldA" are not fetched. Any idea what's wrong? thx

share|improve this question

I believe if you specify 'meta_key' without a 'meta_value' it checks merely for the existence of the meta_key. I haven't tried this but you might be able to specify a 'meta_compare' of '!=' and some random value for 'meta_value' that you know would never actually be set as the value.

$query->set( 'meta_key', 'fieldA' );
$query->set( 'meta_value', 'okeedokeedoggiedaddie' );
$query->set( 'meta_compare', '!=' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
share|improve this answer
thx for the reply but unfortunately it does not do the trick :( .. isn't possible to force LEFT JOIN with the postmeta tablet instead of the INNER JOIN? – yeahman Jul 7 '14 at 4:31
anyone have an idea? I am reluctant in modifying the core meta query class directly – yeahman Jul 8 '14 at 16:25

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.