如何在WordPress中查询带有附加条件的标准和视频Post格式?

How to query Standard and Video Post Format with additional condition in WordPress?

我能够弄清楚如何使用以下代码查询最新的 post 标准格式和视频 post 格式:

    $args = array(
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'posts_per_page'    => 10,
        'tax_query'         => array(
            'relation'    => 'OR',
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' ),
                'operator' => 'IN'
            )
        )
    );

这在 OR 条件下效果很好,因为我试图获得标准和视频 post 格式。但是,我必须添加另一个分类查询,但使用 AND 条件。看起来像这样:

    $args = array(
        'post_status'       => 'publish',
        'orderby'           => 'date',
        'posts_per_page'    => 10,
        'tax_query'         => array(
            'relation'    => 'OR',
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' ),
                'operator' => 'IN'
            ),
            // This should be and
            array(
                'taxonomy' => 'custom-regions',
                'field' => 'slug',
                'terms' => array( 'region-1', 'region-2' ),
                'operator' => 'IN'
            )
        )
    );

据我所知,tax_query 中的 orand 条件无法组合(或者是?)。如果是,是否有办法能够 运行 此查询,以便我可以获得仅属于特定区域的标准或 post 格式?

你可以这样做。

$args = array(
    'post_status'       => 'publish',
    'orderby'           => 'date',
    'posts_per_page'    => 10,
    'tax_query'         => array(
        'relation'    => 'AND',
        array(
            'relation'    => 'OR',
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video', 'post-format-gallery' ),
                'operator' => 'NOT IN'
            ),
            array(
                'taxonomy' => 'post_format',
                'field'    => 'slug',
                'terms'    => array( 'post-format-video' ),
                'operator' => 'IN'
            ),
        ),
        array(
            'taxonomy' => 'custom-regions',
            'field' => 'slug',
            'terms' => array( 'region-1', 'region-2' ),
            'operator' => 'IN'
        )
    )
);