I have a custom WP query that searches a single custom field, using an array.

The custom field is for the country of origin of a post. The array I'm using are the names of the various countries in a region. The objective is to be able to return all posts from a region or continent.

Here is my query:

$args = array(
        'posts_per_page' => '-1',
        'cat' => '4',
        'meta_query' => array(
                'meta_key' => 'Country',
                'meta_value' => $country_search,
                'compare' => 'IN'
    $country_query = new WP_Query( $args );

The $country_search variable is an imploded array: 'Albania','Andorra','Armenia','Austria','Belarus','Belgium', etc.

What happens is that no matter which region I search for, while the correct country array is returned, the query returns all of the posts in the database.

I'm sure it's something simple, but my head is sore from banging it against the keyboard.

I greatly appreciate any help.


1 Answer 1


Thanks for all of the help!

It tured out to be a combination of issues:

  1. Instead of imploding the $country_search_array, it needs to be added as is to the query.
  2. Since it's an array, we can't use the '=' for the compare value. It needs to be 'IN'

I couldn't have figured it out without the assistance I received here. Again, thanks!

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.