簡體   English   中英

WP_Query使用'OR'獲取兩個不同分類的帖子

[英]WP_Query to get posts for two different taxonomies using 'OR'

我想獲得兩種不同分類法的帖子。

我想列出職位只有那些帖子要么有標簽longform post_format是鍵入link

我嘗試過下面的代碼,但它不起作用:

$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
    )
));
$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 );

這應該工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM