以下函数需要放入函数中。php的模板代码。或者在插件中。
function custom_search_query( $request ) {
$query = new WP_Query(); // the query isn\'t run if we don\'t pass any query vars
$query->parse_query($request);
$request[\'post_type\'] = \'LAW\';
// this is the actual manipulation; do whatever you need here
if(isset($_GET[\'search\']))
$options = $_GET[\'search\'];
if (!empty($options)) {
$i = 0;
$request[\'meta_query\'] = array(); // resetting any previously selected meta_queries that might "linger" and cause weird behaviour.
// CAREFUL HERE ^ might not be desired behaviour
foreach($options AS $key => $value) {
$request[\'meta_query\'][$i][\'key\'] = $key;
$request[\'meta_query\'][$i][\'value\'] = array($value);
$request[\'meta_query\'][$i][\'compare\'] = \'IN\';
$request[\'meta_query\'][$i][\'type\'] = \'CHAR\';
$i++;
}
}
return($request);
}
add_filter( \'request\', \'custom_search_query\' );
上述函数假定HTML表单与此类似:
<input type="text|number" name="search[keyname1]" value="value1" />
<input type="text|number" name="search[keyname2]" value="value2" />
它不会对用户输入进行验证(wordpress可能会做一些验证,但如果你这样做会更好)。