这当然是您必须在服务器端解决的问题,只有省略URL中的参数,才能在访问$_GET
大堆
您应该根据是否设置值动态构建参数数组。
示例:
$args = array();
foreach(array(\'s\', \'country\', \'year\') as $key)
{
// if key is available and not empty, include argument in the query
if(isset($_GET[$key]) && trim($_GET[$key]) !== \'\')
{
$args[$key] = $_GET[$key];
}
}
// \'s\' is neccessary for a search query, so only continue if it\'s available
if(isset($args[\'s\']))
{
$the_query = new WP_Query($args);
...
}
Update:
在弄清楚这个国家插件是什么之后,我看了一下。它处理查询,但只检查国家/地区键是否存在,而不检查其是否为空,并对全局查询对象执行此检查,因此即使自定义查询中没有国家/地区,它将出现在全局查询对象中,因为插件将国家/地区键添加到将自动获取的变量列表中(如果您不知道,WordPress将自动为您查询帖子)。您可能想联系插件作者并告诉他有关问题的情况,以便他能够修复它,在此之前,这里有一个针对当前版本的快速而肮脏的修复:
// Hooks...
add_filter(\'posts_join\', \'country_search_join\', 10, 2);
add_filter(\'posts_where\', \'country_search_where\', 10, 2);
add_filter(\'posts_groupby\', \'country_search_groupby\', 10, 2);
add_filter(\'query_vars\', \'country_queryvars\');
add_action(\'init\', \'country_flush_rewrite_rules\');
add_action(\'generate_rewrite_rules\', \'country_add_rewrite_rules\');
add_action(\'plugins_loaded\', \'country_widget_init\');
add_action(\'admin_menu\', \'manage_countries_menu\');
// Join clause for the META table if a country is queried.
function country_search_join($join, &$wp_query)
{
global $wpdb;
if(isset($wp_query->query_vars[\'country\']) && trim($wp_query->query_vars[\'country\']) !== \'\') {
$join .= " left join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id ";
}
return $join;
}
// Add a where clause if a country is queried.
function country_search_where($where, &$wp_query)
{
global $wpdb;
if(isset($wp_query->query_vars[\'country\']) && trim($wp_query->query_vars[\'country\']) !== \'\') {
$where = $where . " and {$wpdb->postmeta}.meta_key = \'Country\' and {$wpdb->postmeta}.meta_value = \'" . $wp_query->query_vars[\'country\'] . "\' ";
}
return $where;
}
// Add a group by clause if a country is queried to make sure posts are only returned once.
function country_search_groupby($groupby, &$wp_query)
{
global $wpdb;
if(!isset($wp_query->query_vars[\'country\']) || trim($wp_query->query_vars[\'country\']) === \'\') {
return $groupby;
}
// Group on post ID
$mygroupby = "{$wpdb->posts}.ID";
// Is this group by already there?
if(preg_match( "/$mygroupby/", $groupby)) {
return $groupby;
}
// Is the group by empty?
if(!strlen(trim($groupby))) {
return $mygroupby;
}
// Append new group by clause.
return $groupby . ", " . $mygroupby;
}
现在我已经了解了插件的具体功能,我建议不要使用自定义查询,WordPress的查询应该可以了,比如这样的查询应该可以:
<?php while ( have_posts() ) : the_post(); ?>
<h3 class="search-title"><?php country_tag($post->ID); ?><a href="#" data-siteurl="<?php bloginfo(\'url\'); ?>" rel="<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>" class="search-post-title"><?php the_title(); ?></a></h3>
<?php endwhile; ?>