首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WordPress中查询带附加条件的标准和Video Post格式?

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

Stack Overflow用户
提问于 2021-04-26 18:50:14
回答 1查看 22关注 0票数 0

我能够用下面的代码来查询标准格式和视频格式的最新帖子:

代码语言:javascript
复制
    $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条件下工作得很好,因为我正在尝试获得标准和视频后期格式。但是,我必须使用AND条件添加另一个分类查询。它看起来像这样:

代码语言:javascript
复制
    $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格式,这些格式只属于某些区域?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-26 20:01:28

你可以做这样的事情。

代码语言:javascript
复制
$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'
        )
    )
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67265276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档