前言
筛选由组合搜索逻辑(用于针对术语的 AJAX 搜索筛选器)返回的术语 ID 列表。允许你在初始搜索查询运行后,从搜索结果中包含或排除术语。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/combined_search/term_ids', function( $term_ids, $term_fields, $meta_fields, $search_term, $filter_id, $query_id ) {
// Example: Exclude 'uncategorized' term (ID: 1) from search results
if ( ( $key = array_search( 1, $term_ids ) ) !== false ) {
unset( $term_ids[ $key ] );
}
return $term_ids;
}, 10, 6 );
参数
$term_ids(array):已找到的术语 ID 数组。$term_fields(array):正在被搜索的术语字段数组(例如 [‘name’, ‘slug’])。$meta_fields(array):正在被搜索的 meta key 数组。$search_term(string):用户输入的搜索词。$filter_id(string):发起搜索的筛选元素的 ID。$query_id(string):正在被筛选的查询循环 ID。
示例代码
筛选返回值
add_filter( 'bricks/combined_search/term_ids', function( $term_ids, $term_fields, $meta_fields, $search_term, $filter_id, $query_id ) {
// Example: Exclude 'uncategorized' term (ID: 1) from search results
if ( ( $key = array_search( 1, $term_ids ) ) !== false ) {
unset( $term_ids[ $key ] );
}
return $term_ids;
}, 10, 6 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/combined_search/term_ids', function( $value, $term_ids ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。