How do I make a meta_query OR relation work?

Here’s are wp_query args:

$args = array(
    'post_type' => 'sportpages'
    ,'post_status' => 'publish'
    ,'posts_per_page' => 1000
    ,'post_parent' => 4738
    ,'meta_query' => array(
        'relation' => 'OR'
        ,array(
            'key' => 'ecpt_sportgender_2'
            ,'value' => 'Boys'
            ,'compare' => 'LIKE'
        )
        ,array(
            'key' => 'ecpt_sportgender_2'
            ,'value' => 'Both'
            ,'compare' => 'LIKE'
        )
    )
);

My results are odd. I’m getting only the posts for Boys unless I swap Boys for Girls, in which case I get Girls. The only way to get “Both” is to make it get ONLY Both, and not do the relationship.

Read More

The order in which I put them in there makes no difference.

What am I missing?

Related posts

Leave a Reply

2 comments

  1. You’ll probably need to do one of the following

    • 'compare' => '=' (or leave it out, as it’s the default)
    • or… 'value' => $wpdb->esc_like( '%Boys%' ) (needs global $wpdb;)
    • or… 'value' => $wpdb->esc_like( 'Boys' ) (needs global $wpdb;)
  2. I figured out my problem. The query WAS returning the proper results, but the results were getting munged in some later code. 🙁

    Thanks for your time pondering this.