WordPress core不支持该参数,但您可以使用\'posts_where\'
:
// require PHP 5.3+
add_action( \'posts_where\', function( $where, $query ) {
if ( isset( $query->query[\'name__in\'] ) && is_array( $query->query[\'name__in\'] ) ) {
global $wpdb;
$names = array_filter( array_map( function( $name ) use( $wpdb ) {
$sane = sanitize_title( $name );
return ! empty( $sane ) ? $wpdb->prepare( \'%s\', $sane ) : NULL;
}, $query->query[\'name__in\'] ) );
if ( ! empty( $names ) ) {
$where .= " AND {$wpdb->posts}.post_name IN (" . implode( \',\', $names ) . ")";
}
}
return $where;
}, PHP_INT_MAX, 2 );
请注意,这将
not 默认情况下使用
get_posts
因为默认情况下,使用该函数会抑制sql筛选器,您需要使用“suppress\\u filters”参数启用它:
这将起作用:
$args = array(
\'post_type\' => \'my-cpt\',
\'name__in\' => array( \'foo\', \'bar\' )
);
$posts = new WP_Query( $args );
这将
not 工作:
$args = array(
\'post_type\' => \'my-cpt\',
\'name__in\' => array( \'foo\', \'bar\' )
);
$posts = get_posts( $args );
这也会起作用:
$args = array(
\'post_type\' => \'my-cpt\',
\'name__in\' => array( \'foo\', \'bar\' ),
\'suppress_filters\' => FALSE // <== enable filters
);
$posts = get_posts( $args );