I have a custom WP query that is returning every post, instead of just the ones being specified by the query. I can’t seem to find anything wrong with the code, so I must be missing something:
//Process incoming variable
if(!empty($_REQUEST['region'])){
$region = $_REQUEST['region'];
} else {
$region = NULL;
}
if (empty($region)) {
echo "No region selected";
//Search Region table to retrieve array of country names
}else{
$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);
}
// Process search reults and format array contents for WP Query
$country_search_array = array();
while($row = mysql_fetch_array($regionresult)){
$country_search_array[] = $row['country'];
}
$country_search = "'".implode("','", $country_search_array)."'";
//Build WP query
$args = array(
'posts_per_page' => '-1',
'cat' => '4',
'meta_query' => array(
array(
'meta_key' => 'Country-2',
'meta_value' => $country_search,
'compare' => '='
)
)
);
$country_query = new WP_Query( $args );
//Some output for troubleshooting purposes
$num = $country_query->post_count;
print_r ($country_search); echo "<br><br>";
print_r ($num);echo " Posts Returned.<br><br>";
// The Loop
while ( $country_query->have_posts() ) : $country_query->the_post();
the_title(); echo"<br>";
endwhile;
// Reset Everything
wp_reset_postdata();
wp_reset_query();
mysql_free_result($regionresult);
If this looks familiar, it’s because I’ve posted other questions related to this project, so I apologize if it seems repetitious. This is the last hurdle, though.
As always, any help is appreciated.
Thanks!
ty
UPDATE – Here is the actual query, as it is generated by print_r:
WP_Query Object
(
[query_vars] => Array
(
[posts_per_page] => -1
[cat] => 4
[meta_query] => Array
(
[0] => Array
(
[meta_key] => Country
[meta_value] => 'Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi','Cape Verde','Central African Republic','Chad','Comoros','Congo','Ivory Coast','Djibouti','Egypt','Eritrea','Ethiopia','Gabon','Gambia','Ghana','Kenya','Lesotho','Liberia','Libya','Madagascar','Malawi','Mauritania','Mauritius','Morocco','Mozambique','Nambia','Niger','Nigeria','Reunion','Rwanda','Sao Tome & Principe','Senegal','Seychelles','Sierra Leone','Somalia','South Africa','Sudan','Swaziland','Tanzania','Togo','Tunisia','Uganda','Zaire','Zambia','Zimbabwe'
[compare] => IN
)
)
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] => gallery
[tag] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[category__in] => Array
(
[0] => 4
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[nopaging] => 1
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => 4
)
[include_children] =>
[field] => term_id
[operator] => IN
)
)
[relation] => AND
)
[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
[0] => Array
(
[meta_key] => Country
[meta_value] => 'Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi','Cape Verde','Central African Republic','Chad','Comoros','Congo','Ivory Coast','Djibouti','Egypt','Eritrea','Ethiopia','Gabon','Gambia','Ghana','Kenya','Lesotho','Liberia','Libya','Madagascar','Malawi','Mauritania','Mauritius','Morocco','Mozambique','Nambia','Niger','Nigeria','Reunion','Rwanda','Sao Tome & Principe','Senegal','Seychelles','Sierra Leone','Somalia','South Africa','Sudan','Swaziland','Tanzania','Togo','Tunisia','Uganda','Zaire','Zambia','Zimbabwe'
[compare] => IN
)
)
[relation] => AND
)
[post_count] => 1047
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] => 1
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => 859f71690a2d92bf36791d7332bf2dbc
[query_vars_changed] =>
[thumbnails_cached] =>
[query] => Array
(
[posts_per_page] => -1
[cat] => 4
[meta_query] => Array
(
[0] => Array
(
[meta_key] => Country
[meta_value] => 'Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi','Cape Verde','Central African Republic','Chad','Comoros','Congo','Ivory Coast','Djibouti','Egypt','Eritrea','Ethiopia','Gabon','Gambia','Ghana','Kenya','Lesotho','Liberia','Libya','Madagascar','Malawi','Mauritania','Mauritius','Morocco','Mozambique','Nambia','Niger','Nigeria','Reunion','Rwanda','Sao Tome & Principe','Senegal','Seychelles','Sierra Leone','Somalia','South Africa','Sudan','Swaziland','Tanzania','Togo','Tunisia','Uganda','Zaire','Zambia','Zimbabwe'
[compare] => IN
)
)
)
[request] => SELECT alere_posts.* FROM alere_posts INNER JOIN alere_term_relationships ON (alere_posts.ID = alere_term_relationships.object_id) WHERE 1=1 AND ( alere_term_relationships.term_taxonomy_id IN (4) ) AND alere_posts.post_type = 'post' AND (alere_posts.post_status = 'publish' OR alere_posts.post_status = 'private') GROUP BY alere_posts.ID ORDER BY alere_posts.post_date DESC
[posts] => Array (â¦
I think you’re confusing old meta parameters with new
meta_query
parameters.meta_key
should be justkey
andmeta_value
should be justvalue
. Refer to WP_Query in Codex for correct syntax.Also,
print_r( $country_query );
will show you the actual SQL query being sent to the database and will show you where you’re going wrong.Thanks for all of the help!
It tured out to be a combination of issues:
$country_search_array
, it needs to beadded as is to the query.
'='
for the compare value. It needs to be'IN'
I couldn’t have figured it out without the assistance I received here. Again, thanks!