Ty Morton
Ty Morton

Reputation: 733

Creating Custom WP Query

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

Upvotes: 0

Views: 255

Answers (2)

Ty Morton
Ty Morton

Reputation: 733

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!

Upvotes: 1

gherkins
gherkins

Reputation: 14983

have you tried

'value' => $country_search,

instead of

'value' => '$country_search',

without the quotes?

Upvotes: 0

Related Questions