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

ty

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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!

share|improve this answer

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.