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'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'
        )
    )
);
$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

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Try following codes:

    $country_search_array = array();
    while($row = mysql_fetch_array($regionresult)){
        $country_search_array[] = $row['country']; // Your country field name
    }
    $country_search = "'".implode("',", $country_search_array)."'";

$args = array(
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'Country',
                'value' => $country_search,
                'compare' => 'IN'
            )
        )
    );
    $query = new WP_Query( $args );
share|improve this answer
    
Well, that is certainly getting me down the road. There was a single quote missing from the implode statement, but this is what $args returns now: Array ([posts_per_page] => -1 [meta_query] => Array ( [0] => Array ([key] => Country [value] => 'Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi','Cape Verde','Central African Republic','Chad',... [compare] => IN ))) –  Ty Morton Jun 24 '12 at 18:14
    
It's still not returning any posts, though. –  Ty Morton Jun 24 '12 at 18:18
    
Here is where I am now: tylonius.com/gallery-test.html –  Ty Morton Jun 24 '12 at 18:52
    
Just remove this line: $country_search = "'".implode("',", $country_search_array)."'"; –  Samiul Amin Shanto Jun 25 '12 at 1:45
    
That's what generates the search array. Without it, value' => $country_search becomes an undeclared value. –  Ty Morton Jun 25 '12 at 1:58
show 4 more comments

You're passing '$country_search' as a literal value, it should be

'value' => $country_search

without single quotes.

Also, I recommend replacing those old mysql query functions with wpdb.

share|improve this answer
    
Yes, I saw that. I actually went back and forth on that trying to determine if that was the problem. –  Ty Morton Jun 24 '12 at 18:07
add comment

The problem was that there were two array objects:

$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 );

It 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',
            'category' => 'Gallery'
    )
);
$query = new WP_Query( $args );
share|improve this answer
add comment

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.