简体   繁体   English

Wordpress:WP_Query 中每种帖子类型的不同选项

[英]Wordpress: Different options per post type in WP_Query

I have two post types (type-A and type-B) and two taxonomies (tax-1 and tax-2), both assigned to each post type.我有两种帖子类型(A 类和 B 类)和两个分类法(tax-1 和 tax-2),它们都分配给每个帖子类型。 This means that posts from type-A can contain terms from tax-1 and tax-2 and posts from type-B can also contain terms from tax-1 and tax-2.这意味着类型 A 的帖子可以包含来自 tax-1 和 tax-2 的条款,来自类型 B 的帖子也可以包含来自 tax-1 和 tax-2 的条款。

I want my WP_Query to output all posts from type-A that contain certain terms of tax-1.我希望我的 WP_Query 输出来自类型 A 的所有帖子,其中包含特定的 tax-1 条款。 But I don't want to output type-B posts that contain these tax-1 terms, which my WP_Query unfortunately does.但我不想输出包含这些 tax-1 条款的 B 类帖子,不幸的是我的 WP_Query 这样做了。 The same should apply to tax-2, whereby only posts from type-B that contain terms from tax-2 should be output.这同样适用于 tax-2,即只输出包含 tax-2 条款的 B 类帖子。

I have already tried to create two $args for this, but I did not manage to merge the two $args.我已经尝试为此创建两个 $args,但我没有设法合并这两个 $args。

function my_function($args) {
    global $post;

    $args = array(
            'post_type' => array('type-A','type-B'),
            'tax_query' => array(
                'relation'  => 'OR',
                 array(
                    'taxonomy' => 'tax-1',
                    'field'    => 'term_id',
                    'terms'    => array(11, 12, 13),
                ),
                array(
                    'taxonomy' => 'tax-2',
                    'field'    => 'term_id',
                    'terms'    => array(21, 22, 23),
                ),
            ),
        );

    return $args;
} 

I have now found a solution myself, which I would like to share here.我现在自己找到了一个解决方案,我想在这里分享。

The idea is to create a query for each of the two post types.这个想法是为两种帖子类型中的每一种创建一个查询。 For each post type I will get the result in a list with the post IDs using wp_list_pluck() .对于每种帖子类型,我将使用wp_list_pluck()在带有帖子 ID 的列表中获得结果。 With array_merge() I merge the lists into one, which can be included in a final query with post__in .使用array_merge()将列表合并为一个,可以使用post__in将其包含在最终查询中。

function my_function($query_args) {
    global $post;
    
    
    $query_args_1 = new WP_Query(array(
            'post_type' => array('type_A'),
            'tax_query' => array(
               'relation'   => 'OR',
                array(
                    'taxonomy' => 'tax_1',
                    'field'    => 'term_id',
                    'terms'    => array(11, 12, 13),
                ),
            ),
));
        $list_id_1 = wp_list_pluck( $query_args_1->posts, 'ID' );
        
        $query_args_2 = new WP_Query(array(
            'post_type' => array('type_B'),
            'tax_query' => array(
                'relation'  => 'OR',
                 array(
                    'taxonomy' => 'tax_2',
                    'field'    => 'term_id',
                    'terms'    => array(21, 22, 23),
                ),
            ),
));
        $list_id_2 = wp_list_pluck( $query_args_2->posts, 'ID' );
        
        $merged_list = array_merge($list_id_1, $list_id_2);
        
        $query_args = array(
            'post_type' => array('type_A', 'type_B'),
            'orderby'   => 'relevance',
            'order'     => 'ASC',
            'post__in'  => $merged_list
);

    return $query_args;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM