我在主题中编辑 wordpress 页面 searchform.php ,添加按子类别过滤。 我在我的function.php中添加了一个add_filter('the_search_query','search_filter')
但是当进行搜索时:结果不关心我的下拉菜单......
==>我的问题:如何使搜索(过滤器)与我的catégory过滤器一起使用?
你有这个:
在 searchform.php :
中输入此代码<?php
$options_type = array(
'name' => 'subcat_type',
'hierarchical' => 1,
'parent' => get_category_by_slug('action-type')->term_id,
'show_option_none' => ("Type d’action"),
'selected' => (isset($_GET['subcat_type']) ? $_GET['subcat_type'] : ''),
'hide_empty' => 0 );
.......
?>
<form method="get" id="search-form" action="<?php echo home_url(); ?>/">
<input class="search-box vntd-boxed-content" name="s" id="s" type="text" value="<?php echo isset($_GET['s']) ? $_GET['s'] : '' ?>" placeholder="<?php _e('Que recherchez vous ?','vntd_renown') ?>">
<?php wp_dropdown_categories($options_type); ?>
<?php wp_dropdown_categories($options_thematiques); ?>
<?php wp_dropdown_categories($options_public); ?>
<?php wp_dropdown_categories($options_territoire); ?>
<input type="hidden" id="my_search" name="my_search" value="c_search" />
<input type="submit" id="searchsubmit" value="Rechercher" />
</form>
在 functions.php :
中输入此代码<?php
// 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['subcat_public']),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $_post['subcat_territoire']),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $_post['subcat_thematiques']),
'operator' => 'IN'
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( $_post['subcat_type']),
'operator' => 'IN'
)
);
$query->set( 'tax_query', $args);
}
return $query;
}
add_filter( 'the_search_query','search_filter');
?>
感谢。
尼科
答案 0 :(得分:1)
已解决:
function recherche_oris($query) {
/*if (!$query->is_search) {
return $query;
} else {
echo "<pre>"; print_r( $query ); echo "</pre>";
die();
}*/
if (isset($_GET['my_search'])){
if (isset($_GET['subcat_type']) && $_GET['subcat_type'] >= 0){
$query->set('category__and', $_GET['subcat_type']);
}
if (isset($_GET['subcat_thematiques']) && $_GET['subcat_thematiques'] >= 0){
$query->set('category__and', $_GET['subcat_thematiques']);
}
if (isset($_GET['subcat_public']) && $_GET['subcat_public'] >= 0){
$query->set('category__and', $_GET['subcat_public']);
}
if (isset($_GET['subcat_territoire']) && $_GET['subcat_territoire'] >= 0){
$query->set('category__and', $_GET['subcat_territoire']);
}
if (isset($_GET['tags'])){
$query->set('tag__and', $_GET['tags']);
}
}
return $query;
}
//hook filters to search
add_filter('pre_get_posts','recherche_oris');