我正在拼命尝试制作一个高级搜索表单,以便通过其元字段获取帖子。我得到了这个:
[更新]:这是正确的代码:
/**
*Registering custom query vars
*
*/
function sy_register_query_var( $vars ){
$vars[] = \'type\';
$vars[] = \'cabins\';
$vars[] = \'base\';
return $vars;
}
add_filter( \'query_vars\', \'sy_register_query_var\' );
<?php
/**
*Registering custom query vars
*
*/
function sy_register_query_var( $vars ){
$vars[] = \'type\';
$vars[] = \'people\';
$vars[] = \'base\';
$vars[] = \'duration\';
return $vars;
}
add_filter( \'query_vars\', \'sy_register_query_var\' );
/**
* Build a custom query based on several conditions
* The pre_get_posts action gives developers access to the $query object by
reference
* any changes you make to $query are made directly to the original object -
no return value is requested
*
*/
function sy_pre_get_posts( $query ){
if( is_admin() || ! $query->is_main_query() ){
return;
}
$query->set( \'type\', get_query_var( \'type\' ) );
$query->set( \'people\', get_query_var( \'people\' ) );
$query->set( \'base\', get_query_var( \'base\' ) );
$query->set( \'duration\', get_query_var( \'duration\' ) );
if( !is_post_type_archive( \'fleet\' ) ){
return;
}
$meta_query = array();
if( !empty( get_query_var( \'type\' ) ) ) {
$meta_query[] = array( \'key\' => \'type\', \'value\' => get_query_var( \'type\' ), \'compare\' => \'LIKE\' );
} elseif ( !empty( get_query_var( \'people\' ) ) ){
$meta_query[] = array( \'key\' => \'people\', \'value\' => get_query_var( \'people\' ), \'compare\' => \'LIKE\' );
} elseif ( !empty( get_query_var( \'base\' ) ) ){
$meta_query[] = array( \'key\' => \'base\', \'value\' => get_query_var( \'base\' ), \'compare\' => \'LIKE\' );
} elseif ( !empty( get_query_var( \'duration\' ) ) ){
$meta_query[] = array( \'key\' => \'duration\', \'value\' => get_query_var( \'duration\' ), \'compare\' => \'LIKE\' );
}
if( count( $meta_query ) > 1 ){
$meta_query[\'relation\'] = \'AND\';
}
if( count( $meta_query ) > 0 ){
$query->set( \'meta_query\', array($meta_query) );
}
}
add_action( \'pre_get_posts\', \'sy_pre_get_posts\', 1 );
function sy_search_form(){
$select_type = \'<select name="type" style="width: 100%">\';
$select_type .= \'<option value="" selected="selected">\' . __( \'Type\', \'syacht\' ) . \'</option>\';
$select_type .= \'<option value="Motor">\' . __( \'Motor\', \'syacht\' ) . \'</option>\';
$select_type .= \'<option value="Sailing">\' . __( \'Sailing\', \'syacht\' ) . \'</option>\';
$select_type .= \'<option value="Ribs">\' . __( \'Ribs\', \'syacht\' ) . \'</option>\';
$select_type .= \'<option value="Caiques">\' . __( \'Caiques\', \'syacht\' ) . \'</option>\';
$select_type .= \'<option value="Speed Boats">\' . __( \'Spead Boats\', \'syacht\' ) . \'</option>\';
$select_type .= \'</select>\' . "\\n";
$select_people = \'<select name="people" style="width: 100%">\';
$select_people .= \'<option value="" selected="selected">\' . __( \'People\', \'syacht\' ) . \'</option>\';
$select_people .= \'<option value="3 or less">\' . __( \'3 or less\', \'syacht\' ) . \'</option>\';
$select_people .= \'<option value="4-6">\' . __( \'4-6\', \'syacht\' ) . \'</option>\';
$select_people .= \'<option value="6 or more">\' . __( \'6 or more\', \'syacht\' ) . \'</option>\';
$select_people .= \'</select>\' . "\\n";
$select_base = \'<select name="base" style="width: 100%">\';
$select_base .= \'<option value="" selected="selected">\' . __( \'Base\', \'syacht\' ) . \'</option>\';
$select_base .= \'<option value="Mykonos">\' . __( \'Mykonos\', \'syacht\' ) . \'</option>\';
$select_base .= \'<option value="Old Port">\' . __( \'Old Port\', \'syacht\' ) . \'</option>\';
$select_base .= \'</select>\' . "\\n";
$select_duration = \'<select name="duration" style="width: 100%">\';
$select_duration .= \'<option value="" selected="selected">\' . __( \'Duration\', \'syacht\' ) . \'</option>\';
$select_duration .= \'<option value="1 to 3 days">\' . __( \'1 to 3 days\', \'syacht\' ) . \'</option>\';
$select_duration .= \'<option value="1 week">\' . __( \'1 week\', \'syacht\' ) . \'</option>\';
$select_duration .= \'<option value="2 weeks">\' . __( \'2 weeks\', \'syacht\' ) . \'</option>\';
$select_duration .= \'</select>\' . "\\n";
$output = \'<form class="advanced-search" action="\' . esc_url( home_url() ) . \'" method="get" role="search">\';
$output .= \'<div class="wrap">\';
$output .= \'<div>\'. $select_type . \'</div>\';
$output .= \'<div>\'. $select_people . \'</div>\';
$output .= \'<div>\'. $select_base . \'</div>\';
$output .= \'<div>\'. $select_duration . \'</div>\';
$output .= \'<input type="hidden" name="post_type" value="fleet" />\';
$output .= \'<div><input type="submit" id="as-submit" class="button gold full" value="Search yachts" /></div>\';
$output .= \'<a href="javascript:void(0)" class="search-hide" title="Hide this box">Hide this box</a>\';
$output .= \'</div></form>\';
return $output;
}
实际上,这种形式有效,但根本不起作用。事实上,搜索页面显示了属于“fleet”自定义帖子类型的所有帖子,它们不会被我设置的搜索词过滤。为什么会这样?错误在哪里?求你了,我已经好几天没试着解决这个问题了。谢谢大家。
P、 s:请不要推荐任何插件。我已经试过很多次了,但都没有成功。
SO网友:Razon Komar Pal
无限制提前搜索方式tax_query
和meta_query
// set all common parameters
$args = array(
\'post_type\' => \'property\',
\'posts_per_page\' => 3,
\'orderby\' => \'ASC\',
\'meta_query\' => [],
\'tax_query\' => []
);
// set meta_query parameters
if ( $property_bedrooms) {
$args[\'meta_query\'][] = [
array(
\'key\' => \'property_bedrooms\',
\'value\' => $property_bedrooms,
\'compare\' => \'IN\',
),
];
}
if ( $property_bathrooms) {
$args[\'meta_query\'][] = [
array(
\'key\' => \'property_bathrooms\',
\'value\' => $property_bathrooms,
\'compare\' => \'IN\',
),
];
}
// set tax_queryparameters
if ( $property_city) {
$args[\'tax_query\'][] = [
array(
\'taxonomy\' => \'property_city\',
\'field\' => \'name\',
\'terms\' => $property_city,
\'operator\' => \'IN\'
),
];
}
if ( $property_type) {
$args[\'tax_query\'][] = [
array(
\'taxonomy\' => \'property_type\',
\'field\' => \'slug\',
\'terms\' => $property_type,
\'operator\' => \'IN\'
),
];
}
$query = new WP_Query( $args );