前言
筛选 Bricks 中可用的多语言(Multilingual)选项。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/helpers/get_posts_args', function( $query_args ) {
// If no post type is provided, treat it as "any" for safety.
$post_type_arg = $query_args['post_type'] ?? 'any';
// Detect whether the caller explicitly requested the attachment post type.
$has_attachment = false;
if ( is_array( $post_type_arg ) ) {
$has_attachment = in_array( 'attachment', $post_type_arg, true );
} else {
$has_attachment = ( $post_type_arg === 'attachment' );
}
// If attachment was explicitly requested, do nothing.
if ( $has_attachment ) {
return $query_args;
}
// Get all public post types once. We only use this when expanding "any".
$public_post_types = array_values(
array_diff(
get_post_types( [ 'public' => true ], 'names' ),
[ 'attachment' ]
)
);
// CASE 1: post_type = 'any'
if ( $post_type_arg === 'any' ) {
$query_args['post_type'] = $public_post_types;
}
// CASE 2: post_type is an array
elseif ( is_array( $post_type_arg ) ) {
$normalized_post_types = array_diff( $post_type_arg, [ 'attachment' ] );
// Defensive handling: if "any" exists in the array, replace it with
// all public post types except attachment.
if ( in_array( 'any', $normalized_post_types, true ) ) {
$normalized_post_types = array_diff( $normalized_post_types, [ 'any' ] );
$normalized_post_types = array_merge( $normalized_post_types, $public_post_types );
}
$query_args['post_type'] = array_values( array_unique( $normalized_post_types ) );
}
return $query_args;
} );
参数
$query_args(array):传递给 get_posts() 的参数数组。
示例代码
筛选返回值
add_filter( 'bricks/helpers/get_posts_args', function( $query_args ) {
// If no post type is provided, treat it as "any" for safety.
$post_type_arg = $query_args['post_type'] ?? 'any';
// Detect whether the caller explicitly requested the attachment post type.
$has_attachment = false;
if ( is_array( $post_type_arg ) ) {
$has_attachment = in_array( 'attachment', $post_type_arg, true );
} else {
$has_attachment = ( $post_type_arg === 'attachment' );
}
// If attachment was explicitly requested, do nothing.
if ( $has_attachment ) {
return $query_args;
}
// Get all public post types once. We only use this when expanding "any".
$public_post_types = array_values(
array_diff(
get_post_types( [ 'public' => true ], 'names' ),
[ 'attachment' ]
)
);
// CASE 1: post_type = 'any'
if ( $post_type_arg === 'any' ) {
$query_args['post_type'] = $public_post_types;
}
// CASE 2: post_type is an array
elseif ( is_array( $post_type_arg ) ) {
$normalized_post_types = array_diff( $post_type_arg, [ 'attachment' ] );
// Defensive handling: if "any" exists in the array, replace it with
// all public post types except attachment.
if ( in_array( 'any', $normalized_post_types, true ) ) {
$normalized_post_types = array_diff( $normalized_post_types, [ 'any' ] );
$normalized_post_types = array_merge( $normalized_post_types, $public_post_types );
}
$query_args['post_type'] = array_values( array_unique( $normalized_post_types ) );
}
return $query_args;
} );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/helpers/get_posts_args', function( $value, $query_args ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。