您可以尝试添加geo_country
作为额外的查询变量,具有:
/**
* Add the \'geo_country\' as a public query variable
*
* @param array $query_vars
* @return array $query_vars
*/
function my_query_vars( $query_vars )
{
$query_vars[] = \'geo_country\';
return $query_vars;
}
add_filter( \'query_vars\', \'my_query_vars\' );
然后设置
pre_get_posts
钩子根据
geo_country
值:
/**
* Filter the feed by the \'geo_country\' meta key
*
* @param WP_Query object $query
* @return void
*/
function my_pre_get_posts( $query )
{
// only for feeds
if( $query->is_feed && $query->is_main_query() )
{
// check if the geo_country variable is set
if( isset( $query->query_vars[\'geo_country\'] )
&& ! empty( $query->query_vars[\'geo_country\'] ) )
{
// if you only want to allow \'alpha-numerics\':
$geo_country = preg_replace( "/[^a-zA-Z0-9]/", "", $query->query_vars[\'geo_country\'] );
// set up the meta query for geo_country
$query->set( \'meta_key\', \'geo_country\' );
$query->set( \'meta_value\', $geo_country );
}
}
}
add_action( \'pre_get_posts\', \'my_pre_get_posts\' );
我想
geo_country
只接受字母数字值
(a-z,A-Z,0-9)
, 如果不是这样,请告诉我。
这适用于我的安装,主题为“二十一二”。