0

I was using the following arguments for a WP_Query for months:

$args = array(
    'post_type' => array( 'posttype1', 'posttype2', 'posttype3', 'posttype4' ),
    'posts_per_page' => -1,
    'meta_query' => array(
        'relation' => 'OR',
        array('key' => 'field1_0_subfield'),
        array('key' => 'field2'),
        array('key' => 'field3_0_subfield'),
        array('key' => 'field4' )
    ),
    'meta_key' => 'meta_value',
    'orderby' => 'meta_value',
    'order' => 'ASC'
);

There are four different post types querried (posttype1, posttype2, posttype3, posttype4). The meta_query querries ACF (Advanced Custom Fields) fields (field2, field4) as well as ACF subfields (field1_0_subfield, field3_0_subfield) containing dates each. So the entries in the four post types are sorted by date ascending chronologically. With the update to Wordpress 4.1 the query stopped working and i have no idea about the reason.

1
  • Yep i am aware that the wp_meta_query has changed quite a bit, basically it is more ore less a complete rewrite of the function. But in my present example nesting wouldn't make sense like shown in the nested meta query examples - i just have 4 single fields/keys with an OR relation. Basically it should just go on working since the syntax hasn't changed as well imho :/
    – rkoller
    Dec 20, 2014 at 15:07

1 Answer 1

2

The problem is with the arg 'meta_key' => 'meta_value' - meta_key isn't needed if you have a meta_query arg (despite what the codex appears to say) and its value here doesn't make sense anyway - so just remove it.

However it does point out a behavioural change in 4.1 as before the meta_key was ORed with the meta_query keys (hence your query worked as the meta_key = 'meta_value' clause was basically a no-op) but now it's ANDed and so fails. WP 4.0.1:

wp_postmeta.meta_key = 'meta_value' OR wp_postmeta.meta_key = 'field1_0_subfield' OR wp_postmeta.meta_key = 'field2' ...

WP 4.1 (mt1 being an alias of wp_postmeta):

wp_postmeta.meta_key = 'meta_value' AND (mt1.meta_key = 'field1_0_subfield' OR mt1.meta_key = 'field2' ...)

(PS an easy way to see what SQL is being produced by a query is to look at $query->request.)

1
  • Thanks a lot for the clarification - explained things perfectly! I think the old Codex page i've got the info from was a bit misleading. Works now. Thanks again!
    – rkoller
    Dec 21, 2014 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.