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

1 Answer 1

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 at 4:31
    
anyone have an idea? I am reluctant in modifying the core meta query class directly –  yeahman Jul 8 at 16:25

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.