看起来这个问题已经问了几个月了,但这个问题很好,所以我要把它从坟墓里挖出来。
我解决这个问题的方法是用pre_get_posts
根据提供的信息过滤并添加元查询。这里是解决方案的基本快照,它可以成为插件,也可以加入主题的功能。php文件:
<?php
/**
* Add a parameter for a custom field search
*/
add_filter(\'query_vars\', \'wpse_35639_search_queryvars\' );
function wpse_35639_search_queryvars( $qvars ) {
$qvars[] = \'bedrooms\';
return $qvars;
}
/**
* Intercept the posts query to add in our meta query if necessary
*/
add_action(\'pre_get_posts\',\'wpse_35639_intercept_search\');
function wpse_35639_intercept_search() {
global $wp_query;
if ( ! is_admin() && isset($wp_query->query_vars[\'bedrooms\']) && is_numeric($wp_query->query_vars[\'bedrooms\']) ) {
# Limit the search to the floor_plan custom post type
$wp_query->set(\'post_type\', \'floor_plan\');
# This may seem unconventional, but we\'re setting that this is a search
# even though WP doesn\'t recognize it as one. This is to leverage the search template
$wp_query->is_search = true;
# Set the meta query comparison
$wp_query->set(\'meta_query\', array(
array(
\'key\' => \'number_of_bedrooms\',
\'value\' => $wp_query->query_vars[\'bedrooms\'],
\'compare\' => \'=\',
\'type\' => \'NUMERIC\'
)
);
}
}
?>
注意,这只解决了搜索的自定义字段部分。按页面搜索要容易得多(我想你已经解决了这个问题)。