【问题标题】:WP_Query to get posts for two different taxonomies using 'OR'WP_Query 使用“或”获取两种不同分类法的帖子
【发布时间】:2017-08-28 13:19:48
【问题描述】:

我想获取两种不同分类法的帖子。

我只想列出那些帖子或者有标签longform或者post_formatlink类型的帖子。

我已经尝试了下面的代码,但它不起作用:

$query = new WP_Query(array(
   'relation' => 'OR',
    'posts_per_page' => 2,
    array(
        'taxonomy' => 'post_format',
        'field' => 'slug',
        'terms' => array( 'link' ) // Single terms as string - multiple as array
    ),
    array(
        'taxonomy' => 'Tag',
        'field' => 'id',
       'tag__in' => array('16') // 16 is the code for tag longform
    )
));

【问题讨论】:

    标签: php wordpress


    【解决方案1】:
    $terms = array('link');
    $tax = array(16)
    
     $args = array( 
                               'post_type' => 'post',
                               'tax_query' => array(
                                'relation' => 'OR',
                                        array(
                                            'taxonomy' => 'post_format',
                                            'field'    => 'slug',
                                            'terms'    => $terms,
                                        ),
                                        array(
                                            'taxonomy' => 'space',
                                            'field'    => 'id',
                                            'tag__in'    => $tax,
                                        ),
                                ),
                               'posts_per_page' => 2
                            );
    
    
                        $the_query = new WP_Query( $args );
    

    这应该可行!

    【讨论】:

    • 您能否使用我在问题中提到的确切 ID 和 slug 编辑您的答案,因为我对您的答案有些困惑。
    • @Hussain 它不起作用,实际上我在 function.php 文件中创建了一个短代码来列出帖子,不知道错误在哪里,如果我输入单个条件,例如 'tag' => 'longform' 然后它的工作.
    猜你喜欢
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多