前言
筛选 Query Filters 索引器用于查找需要索引的对象(文章、术语、用户)的查询参数。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/query_filters/index_args', function( $args, $filter_source, $filter_settings, $query_type ) {
// Example: Include 'private' posts when indexing
if ( $query_type === 'wp_query' ) {
$args['post_status'] = [ 'publish', 'private' ];
}
return $args;
}, 10, 4 );
参数
$args(array):查询参数(用于 WP_Query、WP_Term_Query 或 WP_User_Query)。$filter_source(string):筛选数据来源(例如 wpField、customField)。$filter_settings(array):正在被索引的筛选元素的设置。$query_type(string):正在运行的查询类型(wp_query、wp_term_query、wp_user_query)。
示例代码
筛选返回值
add_filter( 'bricks/query_filters/index_args', function( $args, $filter_source, $filter_settings, $query_type ) {
// Example: Include 'private' posts when indexing
if ( $query_type === 'wp_query' ) {
$args['post_status'] = [ 'publish', 'private' ];
}
return $args;
}, 10, 4 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/query_filters/index_args', function( $value, $args ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。