如何在WordPress中查询标准和视频Post格式?

How to query Standard and Video Post Format in WordPress?

我正在尝试根据 post 格式查询 post。目前,我的网站上有视频、图库和标准 post 格式。单独查询这些 post 格式非常容易。但是,我有 10 post 标准和视频 post 格式的税务查询组合,我的查询失败了。

这是我现在拥有的代码:

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

$query = new WP_Query( $args );

我认为,您应该将 'relation' => 'OR', 放入 'tax_query'-数组。

您在错误的地方添加了 'relation' => 'OR'。它应该在 tax_query 数组中。检查下面的代码。

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

$query = new WP_Query( $args );

有用的链接