获取来自多个术语的帖子,但要确保它们是唯一的


Get posts from multiple terms but ensure they are unique

我有下面的循环,它正在执行以下操作:

  1. 循环浏览我的所有自定义分类术语,看看复选框"主页上的类别"被选中,它会打乱那些选定
  2. 然后,它循环浏览所选的术语,并为每个术语随机抽取1个自定义帖子
  3. 然后,它输出一个框,其中包含术语名称和所选帖子的图像

我遇到的问题是,帖子被标记了多个术语,所以它很可能会在不同的术语中选择同一个帖子。

如何确保所选的帖子是唯一的?

<?php $terms = get_terms('project-categories', array(
    'orderby'    => 'rand',
    'hide_empty' => 1,
    )
 );
shuffle($terms);
foreach ( $terms as $term ) {
    if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
        if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
            $termid = $term->term_id;
            $termname = $term->name;
            $termslug = $term->slug;
            $args = array(
                'posts_per_page' => 1,
                'orderby' => 'rand',
                'post_type' => 'projects',
                'post_status' => 'publish',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'project-categories',
                        'field' => 'id',
                        'terms' => $termid
                    )
                )
            );
            $projects = get_posts( $args );
            foreach ( $projects as $post ) {
                setup_postdata($post); ?>
                <a class="service" href="<?php echo $termslug; ?>">
                    <span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
                    <span class="service-title"><?php echo $termname; ?></span>
                </a>
            <?php }
            wp_reset_postdata();
        }
    }
} ?> 

构建一个使用过的post-id数组,然后使用post__not_in查询参数从将来的查询中排除使用过的帖子。

<?php
    $terms = get_terms('project-categories', array(
        'orderby'    => 'rand',
        'hide_empty' => 1
        )
    );
    $used_posts = array();
    shuffle($terms);
    foreach ( $terms as $term ) {
        if(get_field('category_on_homepage', 'project-categories_' . $term->term_id)) {
            if (in_array('Yes', get_field('category_on_homepage', 'project-categories_' . $term->term_id))) {
                $termid = $term->term_id;
                $termname = $term->name;
                $termslug = $term->slug;
                $args = array(
                    'posts_per_page' => 1,
                    'orderby' => 'rand',
                    'post_type' => 'projects',
                    'post_status' => 'publish',
                    'post__not_in' => $used_posts,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'project-categories',
                            'field' => 'id',
                            'terms' => $termid
                        )
                    )
                );
                $projects = get_posts( $args );
                foreach ( $projects as $post ) {
                    setup_postdata($post);
                    array_push($used_posts, $post->ID);
                    ?>
                        <a class="service" href="<?php echo $termslug; ?>">
                            <span class="service-image" style="background-image:url('<?php echo the_field('thumb_image'); ?>');"></span>
                            <span class="service-title"><?php echo $termname; ?></span>
                        </a>
                    <?php
                }
                wp_reset_postdata();
            }
        }
    }
?>