【问题标题】:For each loop results in duplicate post display when post has more than one category当帖子具有多个类别时,每个循环都会导致重复的帖子显示
【发布时间】:2020-05-06 14:49:24
【问题描述】:

我有以下代码:

<div class="documents-posts">
    <?php if (!empty($posts)) : ?>
        <?php foreach ($posts as $post) : ?>
            <?php set_query_var( 'taxonomy', $taxonomy ); ?>
            <?php get_template_part('document'); ?>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

只要每个帖子只有一个类别,它就可以正常工作。当我为帖子分配第二个类别时,帖子会显示两次。如果我让它有 3 个类别,则帖子会显示三次……为什么?

更新: 声明 $posts 变量

$post_type = 'press-release';
$taxonomy = 'press-release-types';
$categories = $xxxxxx->get_categories($taxonomy);
$posts = $xxxxxx->get_posts_ordered_by_categories($post_type, $taxonomy, $categories);

set_query_var( 'posts', $posts );
set_query_var( 'categories', $categories );
set_query_var( 'taxonomy', $taxonomy );

更新 2:来自 Helper php

public function get_posts_ordered_by_categories($post_type, $taxonomy, $categories) {
    $posts = [];
    foreach ($categories as $category) {
        $category_posts = $this->get_posts_from_category($post_type, $taxonomy, $category->term_id);
        $posts = array_merge($posts, $category_posts);
    }
    return $posts;
}

protected function get_posts_from_category($post_type, $taxonomy, $category_id) {
    return get_posts([
        'tax_query' => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'term_id',
                'terms'    => (int) $category_id,
            )
        ),
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
        'post_status' => 'publish',
        'post_type' => $post_type,
        'suppress_filters' => false,
    ]);
}

【问题讨论】:

  • 如何声明 $posts 变量? WP_查询? get_posts() ?
  • @ninja:我希望这会有所帮助。我不是 php 专业人士……
  • 无论get_posts_ordered_by_categories 来自哪里,我认为显示重复是对此的有效行为(如果帖子属于两个或多个类别并且查看者查看一个特定类别,他应该找到所有想要的那里的帖子)。如果您不想要这种行为,则必须自己过滤掉重复项。你为什么首先使用这个get_posts_ordered_by_categories 而不是只查询所有“新闻稿”类型的帖子?
  • 在我看来,这是一种不受欢迎的行为。因为我在上面有一个过滤类别。为什么我要显示相同的帖子两次?过滤一个或另一个类别时,结果中应包含 2 个类别的帖子。但不是每个过滤两次(甚至根本不过滤)。我错了吗?
  • @Damocles:看我的更新 2...谢谢!

标签: wordpress loops post foreach duplicates


【解决方案1】:

get_posts_ordered_by_categories 中,您从该类别中获取所有帖子并将其放入帖子数组中。您必须检查该数组中是否存在帖子。例如:

public function get_posts_ordered_by_categories($post_type, $taxonomy, $categories) {
    $posts = [];
    foreach ($categories as $category) {
        $category_posts = $this->get_posts_from_category($post_type, $taxonomy, $category->term_id);
        foreach ($category_posts as $post) {
            $posts[$post->ID] = $post;
        }
    }
    return $posts;
}

这样您将获得独特帖子的列表。

【讨论】:

  • 非常感谢!我试过这个函数,但它抛出了一个“严重错误”(在里面的第二个循环内)
  • 我不知道get_posts_category 究竟返回了什么。你能发布print_r($this-&gt;get_posts_from_category)的结果吗?
  • 对不起我的无知。在我的代码中,我应该把“print_r…”这一行放在哪里?
【解决方案2】:
public function get_posts_ordered_by_categories($post_type, $taxonomy, $categories) {
    $posts = [];
    foreach ($categories as $category) {
        $category_posts = $this->get_posts_from_category($post_type, $taxonomy, $category->term_id);
       print_r($category_posts);
    }
    return $posts;
}

发布此代码的结果。

【讨论】:

  • 非常感谢您的帮助,非常感谢,但我不能这样做 :-( 首先:因为它大约 2 米长,其次它包含我无法公开的客户数据。它以:数组([0] => WP_Post 对象([ID] => 2810 [post_author] => 8 [post_date] => 2019-09-18 21:45:20 [post_date_gmt] => 2019-09- 18 19:45:20 [post_content] => [post_title] => ...
  • 我忘了写我只需要这个样本。您提供的样本就足够了。我编辑我的第一个答案。它现在应该可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多