简体   繁体   中英

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. 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.

I want my WP_Query to output all posts from type-A that contain certain terms of tax-1. But I don't want to output type-B posts that contain these tax-1 terms, which my WP_Query unfortunately does. The same should apply to tax-2, whereby only posts from type-B that contain terms from tax-2 should be output.

I have already tried to create two $args for this, but I did not manage to merge the two $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() . With array_merge() I merge the lists into one, which can be included in a final query with 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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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