Support » How-To and Troubleshooting » pre_get_posts meta_query nested array not working

pre_get_posts meta_query nested array not working

  • wendallsan2


    In my pre_get_posts function, setting the meta_query returns no results if it is a nested array. It works fine with a single (non-nested) array passed, but this is not how the documentation explains this feature and I actually do need to to filter with multiple meta_queries.

    This works, even though according to the documentation it should not, as I am not setting the meta_query to a nested array:

    $meta_query = array( 'key' => 'author', 'value' => $member_ids, 'compare' => 'IN' );
    $query->set('meta_query', $meta_query);

    This does not work (returns no results, even though I have posts that match both meta_query criteria):

    $meta_query = array();
    $meta_query[] = array( 'key' => 'author', 'value' => $member_ids, 'compare' => 'IN' );
    $meta_query[] = array( 'key' => 'mpl_pal_share_pal', 'value' => 1 )
    $query->set('meta_query', $meta_query);

    This also does not work (this is the same thing as the first example above, which does work, except now it is nested in another array, which is how the documentation says it needs to be):

    $meta_query = array();
    $meta_query[] = array( 'key' => 'author', 'value' => $member_ids, 'compare' => 'IN' );
    $query->set('meta_query', $meta_query);

    Again, I expect this last example should work exactly like the first example, in that it has the same exact meta_query, and the only difference is that it is nested in an array. According to the documentation (http://codex.wordpress.org/Class_Reference/WP_Query) the meta_query needs to be a nested array, but this is absolutely not working for me. Any nested array return no results. If it is not a nested array, it works great, but I then cannot query using multiple meta_queries, which is what I need. Any help?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Ross Wintle


    I seem to be struggling with this too. Do you know if it was logged as a bug or anything? Seems to be no response!

    Forum Moderator


    If you require assistance then, as per the Forum Welcome, please post your own topic instead of tagging onto someone else’s topic.

    I am now closing this 4 month old topic as it references an older version of WordPress.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘pre_get_posts meta_query nested array not working’ is closed to new replies.