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