需要按选定字词顺序显示节目

时间:2019-02-26 09:57:03

标签: php wordpress

我需要按选定的条款显示帖子

示例:

'taxanomy' = 'My_Taxanomy',
'terms' = array(1,2,3),

所以,首先我要显示所有带有术语1的帖子,然后显示所有带有术语2的帖子...。当然,所有帖子都列在一个列表中。

1 个答案:

答案 0 :(得分:0)

$pages = get_posts(array(
  'post_type' => 'page',
  'numberposts' => -1,
  'tax_query' => array(
    array(
      'taxonomy' => 'taxonomy-name',
      'field' => 'id',
      'terms' => array(1,2,3) // Where term_id of Term 1 is "1".
      'include_children' => false
    )
  )
));

尝试一下

也可能是这样

$terms = get_terms('taxonomy-name');
foreach($terms as $term) {
    $posts = get_posts(array(
            'tax_query' => array(
                array(
                    'taxonomy' => 'taxonomy-name',
                    'field' => 'slug',
                    'terms' => $term->slug
                )
            ),
            'numberposts' => -1
        ));
    foreach($posts as $post) {
        // do what you want here
    }
}