不要在WordPress循环中返回重复的标题

不要在WordPress循环中返回重复的标题,wordpress,loops,unique,Wordpress,Loops,Unique,我有一个标准循环,用于返回自定义帖子类型的标题: <ul> <?php $args = array( 'post_type' => 'food_types', 'posts_per_page' => -1 ); $query = new WP_Query($args); while ($query->have_posts())

我有一个标准循环,用于返回自定义帖子类型的标题:

    <ul>
    <?php 
        $args = array(
        'post_type'         => 'food_types',
        'posts_per_page'    => -1
        );
        $query = new WP_Query($args);
        while ($query->have_posts()) : $query->the_post(); ?>
        <li><?php echo the_title();?></li>
        <?php endwhile;
        wp_reset_postdata();
    ?>
    </ul>
…则循环应仅返回以下内容:

Tomato
Orange
Apple
Egg
Banana

我希望这是有意义的。

数据库中的复制是否正常,如果不正常,则可能需要运行脚本来删除重复项,因为这可能会影响性能

如果列表中没有太多的项目,那么当前代码的快速解决方案可以简单地将每个标题添加到数组中,然后继续while循环(如果之前添加了该项目)

用途:及

所以像这样的。。注:这只是概念上的。。。我没有测试代码:

<ul>
<?php 
    $args = array(
    'post_type'         => 'food_types',
    'posts_per_page'    => -1
    );
    $query = new WP_Query($args);
    $list = array();
    while ($query->have_posts()) : $query->the_post(); 
    if(in_array(get_the_title(), $list)){ continue; }
    $list[] = get_the_title();
    ?>
    <li><?php echo get_the_title();?></li>
    <?php endwhile;
    wp_reset_postdata();
?>
</ul>

*修改为使用“获取”标题,如您在“注释”中所解释的,该标题不需要回显。检查:

<ul>
    <?php 
    $args = array(
        'post_type'         => 'food_types',
        'posts_per_page'    => -1
    );

    $query = new WP_Query($args);
    while( $query->have_posts() ) : $query->the_post(); ?>
        <li><?php the_title();?></li>
    <?php endwhile;
     wp_reset_postdata(); ?>
</ul>

如果您只担心水果名称,那么为其创建一个单独的数据集,甚至不必麻烦循环所有帖子,并且消除了在循环中跟踪名称的负担,这可能是有意义的

编辑:将此功能移动到一个函数中是有意义的,这样您就可以方便地在任何地方重用它

// functions.php
function getFruits() {
    $query = new WP_Query([
        'post_type'       => 'food_types',
        'posts_per_page'  => -1
    ]);

    // When to use wp_reset_postdata(): https://wordpress.stackexchange.com/a/144344/145214
    wp_reset_postdata();

    // Extracts out just post_titles and makes new array
    $fruits = array_column( $query->posts, 'post_title'); // PHP7+

    // Gets unique values
    $fruits = array_unique( $fruits );

    return $fruits;
}

// template
$fruits = getFruits();

// Then all you have to do is loop that list
foreach($fruits as $fruit) : ?>

    <li>
        <?= $fruit ?>
    </li>

<?php endforeach;

这实际上几乎奏效了。我只是需要换衣服来取而代之。除此之外,发生了一个我没想到的问题。例如,如果我将帖子限制为10篇,有时它可能只返回一个结果,因为最近10篇帖子的标题相同。不管怎样,我都会把你的帖子标记为正确答案。我已经编辑过了,因为它很有效。但是如果你对这个新问题有什么建议,我洗耳恭听。这取决于情况。这只是你质疑的标题吗?如果您喜欢SQL,那么您可以使用wpdb创建自己的查询,而不是使用WP_查询,并从$wpdb->posts中选择DISTINCT post_title,例如。。。WordPress还有一个名为posts_distinct的命令,但它似乎仅限于某些情况,如果你想检查一下,还是没有回答问题。你的回答更适合作为评论。标题不需要回应。检查:->正确,我的错。我没注意到,这是个好办法。我只想补充一点,如果使用get_posts,只需在array_column函数中放入$query。
// functions.php
function getFruits() {
    $query = new WP_Query([
        'post_type'       => 'food_types',
        'posts_per_page'  => -1
    ]);

    // When to use wp_reset_postdata(): https://wordpress.stackexchange.com/a/144344/145214
    wp_reset_postdata();

    // Extracts out just post_titles and makes new array
    $fruits = array_column( $query->posts, 'post_title'); // PHP7+

    // Gets unique values
    $fruits = array_unique( $fruits );

    return $fruits;
}

// template
$fruits = getFruits();

// Then all you have to do is loop that list
foreach($fruits as $fruit) : ?>

    <li>
        <?= $fruit ?>
    </li>

<?php endforeach;