[英]Wordpress : why my custom sub-category search filter don't work?
我在主题中编辑wordpress页面searchform.php以按子类别添加过滤器 。 然后在我的function.php中添加一个add_filter('the_search_query','search_filter')
但是在执行搜索时:结果不在乎我的下拉列表...
==>我的问题:如何使搜索(过滤器)与我的分类过滤器一起使用?
嗨,有这个:
这段代码在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');
?>
谢谢。
尼科
解决 :
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.