首先,您必须提供下拉列表名称,以便:
<?php $media = array(
\'name\' => \'subcat\',
\'hierarchical\' => 1,
\'parent\' => get_cat_id(\'Media\'),
\'show_option_none\' => (\'All Media\'),
\'hide_empty\' => 0 );
?>
<form method="get" id="searchform" action="<?php bloginfo(\'url\'); ?>/">
<div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<?php wp_dropdown_categories(\'name=maincat&show_option_none=All Category\'); ?>
<?php wp_dropdown_categories($media); ?>
<input type="hidden" id="my_search" name="my_search" value="c_search" />
<input type="submit" id="searchsubmit" value="search" />
</div>
</form>
然后创建搜索筛选器
// Define search filter
function search_filter( $query ) {
// only modify your custom search query.
if ( $query->is_search && $_post[\'my_search\'] == "c_search") {
$args = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => array( $_post[\'maincat\']),
\'operator\' => \'IN\'
),
array(
\'taxonomy\' => \'category\',
\'field\' => \'id\',
\'terms\' => array( $_post[\'subcat\']),
\'operator\' => \'IN\'
)
);
$query->set( \'tax_query\', $args);
}
return $query;
}
// The hook needed to search_filter
add_filter( \'the_search_query\',\'search_filter\');