0

I've hit some sort of stumbling bock here, and I can't find my way over it.

I'm trying to craft a custom WP query that searches for posts by country of origin, selecting multiple countries at once.

I have a table that maps countries to regions, so the initial query will search for the selected region, and return all of the countries in that region as an array. For example, if you select Africa, it will return an array of countries within Africa.

if(!empty($_REQUEST['region'])){
     $region = $_REQUEST['region'];

    $regionresult = mysql_query("SELECT * FROM gallery_regions WHERE region='$region'") or die(mysql_error());
    $num_rows = mysql_num_rows($regionresult);

    if (!$regionresult) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        $message .= 'Whole query: ' . $query;
        die($message);
    }
} else {
     $region = NULL;
}

That part works.

Now, I want to use the returning array to search my WP posts for any that originated from any of the countries in the array. This is the part I can't get to work:

$country_search = array(mysql_fetch_array($regionresult));
$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'Country',
            'value' => '$country_search',
            'compare' => 'IN',
            'category' => 'Gallery'
        )
    )
);
$query = new WP_Query( $args );

I get no results back. In fact, if include echo "$args";, what I get back is simply the word "Array." I also only want it to return results from a specific category.

I'm sure that I'm overlooking something simple. Can anyone help?

Thanks!

ty

8
  • 1
    Have you tried print_r($args) instead of echo $args? Commented Jun 24, 2012 at 17:33
  • That returns this: "Array ( [posts_per_page] => -1 [meta_query] => Array ( [0] => Array ( [key] => Country [value] => $country_search [compare] => IN ) ) )" Commented Jun 24, 2012 at 17:36
  • 'value' => '$country_search'should be 'value' => $country_search without quotes. Commented Jun 24, 2012 at 17:42
  • That yields me this:Array ([posts_per_page] => -1 [meta_query] => Array([0] => Array([meta_key] => Country [meta_value] => Array ([0] => Array([0] => 4 [id] => 4 [1] => Algeria [country] => Algeria [2] => AF [region] => AF ) ) [compare] => IN ) ) ) Commented Jun 24, 2012 at 17:45
  • Try to print_r($country_search) to see the array. Commented Jun 24, 2012 at 17:46

2 Answers 2

1

I thought the problem was that there were two array objects, and that this:

$country_search = array(mysql_fetch_array($regionresult));
$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => 'Country',
            'value' => '$country_search',
            'compare' => 'IN',
        )
    )
);
$query = new WP_Query( $args );

Should have been this:

$country_search = array(mysql_fetch_array($regionresult));
$args = array(
    'posts_per_page' => -1,
    'meta_query' => array(
            'key' => 'Country',
            'value' => $country_search,
            'compare' => 'IN',
    )
);
$query = new WP_Query( $args );

But then, instead of returning no posts, it returned all of them. Grumble!

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!

Sign up to request clarification or add additional context in comments.

Comments

0

have you tried

'value' => $country_search,

instead of

'value' => '$country_search',

without the quotes?

1 Comment

Yes, I kept switching back and forth. Here's where I am right now:morepositive.com/gallery-test/?region=AF

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.