按类别名称和自定义分类的QUERY_POST

时间:2015-12-13 作者:delueg

我想显示按category\\u名称和自定义分类术语筛选的帖子:

我有

function channel_init() {
// create a new taxonomy
register_taxonomy(
    \'channel\',
    \'post\',
    array(
        \'label\' => __( \'Channel\' ),
        \'rewrite\' => array( \'slug\' => \'channel\' ),
        \'hierarchical\' => true,
        \'capabilities\' => array(
            \'manage__terms\' => \'edit_posts\',
            \'edit_terms\' => \'manage_categories\',
            \'delete_terms\' => \'manage_categories\',
            \'assign_terms\' => \'edit_posts\'
        )
    )
);

}
add_action( \'init\', \'channel_init\' );
假设我有一个频道术语“新闻”。还有一个catgory\\u名称“music”-现在我想询问:

query_posts( array( \'channel\' => \'news\', \'category_name\' => \'music\') );
频道被忽略,仅过滤类别。

1 个回复
最合适的回答,由SO网友:Por 整理而成

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'channel\',
            \'field\'    => \'slug\',
            \'terms\'    => array( \'news\', \'comedy\' ), //this is by slug
        ),
        array(
            \'taxonomy\' => \'category\',
            \'field\'    => \'term_id\',
            \'terms\'    => array( 103, 115, 206 ), //this is by id
        )
    )
);

$query = new WP_Query( $args );
如果您使用WP\\u Query而不是Query\\u posts,那就太好了。但结果是一样的。WP\\u查询更灵活:)。别忘了更深入地了解query.