我有一个自定义的WP查询,它返回每个帖子,而不仅仅是查询指定的帖子。我似乎找不到代码有什么问题,所以我肯定遗漏了什么:
//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);
如果这看起来很熟悉,那是因为我已经发布了与此项目相关的其他问题,所以如果这看起来重复,我很抱歉。不过,这是最后一个障碍。
一如既往,我们非常感谢您的帮助。
谢谢
泰
更新-这是实际查询,因为它是由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 (…