get_posts()
将数据传递给WP_Query
作为cat
论点as you can see in the source:
1863 if ( ! empty($r[\'category\']) )
1864 $r[\'cat\'] = $r[\'category\'];
资料来源中还有一条相关注释:
1835 * @type int|string $category Category ID or comma-separated list of IDs (this or any children).
1836 * Is an alias of $cat in WP_Query. Default 0.
That is going to match up to this usage:
$query = new WP_Query( \'cat=2,6,17,38\' );
如果运行该查询,您将看到它是
OR
关系:
$query = new WP_Query( \'cat=2,6,17,38\' );
var_dump($query->request);
更具体地说,它使用MySQL的
IN()
语法。
你可以把任何事情都推过去get_posts()
这将与WP_Query
所以这应该是可行的:
$query = get_posts( \'category_name=staff+news\' );
或者这个:
$query = get_posts( array(\'category_name\' => \'staff+news\' );
然而,跳过
get_posts()
功能和使用
WP_Query
直接地
get_posts()
实际上只是一个薄薄的包装,让我觉得代码膨胀了。分类查询将为您提供所需的所有控件:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'terms\' => array( 2,6,17,38 ),
\'operator\' => \'AND\'
),
),
);
$query = new WP_Query( $args );