Don't return duplicate title in WordPress loop

to don something
don synonym
don in english
don urban dictionary
don clothing
don meaning slang
don medical abbreviation
don meaning italian

I have this standard loop for returning titles of a custom post type:

    <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>

The problem is that there are often posts with the same title.

I need my loop modified so that there is only one instance of any given post title returned in the loop.

For example, if there are posts with these titles...

Tomato
Orange
Orange
Apple
Apple
Egg
Banana
Banana

...then the loop should only return this:

Tomato
Orange
Apple
Egg
Banana

I hope that makes sense.

If you are only worried about fruit names, it may make sense to create a separate data set for it and not even bother looping all posts and removes the burden of tracking names inside the loop.

Edit: It makes sense to move this functionality into a function so you can easily reuse it anywhere.

// 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;

Don, (Entry 1 of 4) transitive verb. 1 : to put on (an article of clothing) donned his hat and gloves. 2 : to wrap oneself in : take on sense 3a the donning of new and more tyrannous moralities— Edward Sapir. Don-O-Mite Disinfectant Cleanerfor use on hard surfaces, ideal for spot cleaning & reduces cross.

the_title() does not require to echo. check:

<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>

Don, DCP DeSilva (Boman Irani) sees a way to bring to justice the feared head of a criminal empire by recruiting a man named Vijay, who looks exactly like the crime boss (Shah Rukh Khan). The ruse works too well, and soon Vijay finds his life in danger Don definition, Mr.; Sir: a Spanish title prefixed to a man's given name. See more.

Is duplication in the database ok, if not, then you might want to run a script to remove duplicates, as it can result in a performance hit.

If there are not too many items in the list, then a quick solution for your current code could simply to add each title to an array, and then continue the while loop if the item was previously added.

Use: https://www.php.net/manual/en/function.array-push.php and https://www.php.net/manual/en/function.in-array.php

So something like this.. NB: this is just in concept... I did not test the code.:

<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>

*Modified to use get_the_title() as you explained in comments

Don, verb (used with object), donned, don�ning. to put on or dress in: to don one's clothes. Origin of� 1. Don (also dōn) Used as a courtesy title before the name of a man in a Spanish-speaking area. 2.

Don, A man who is very sexy and powerfull. This man can control the hearts of all women, while being caring and still sexy. A don is a guy that everyone wants to be like� Dôn, in Celtic mythology, leader of one of two warring families of gods; according to one interpretation, the Children of Dôn were the powers of light, constantly in conflict with the Children of Llyr, the powers of darkness. In another view, the conflict was a struggle between indigenous gods and those of an invading people.

don, noun. 1British A university teacher, especially a senior member of a college at Oxford or Cambridge. 'He worked easily with the many newcomers into his� Don, and dom, is derived from the Latin Dominus: a master of a household, a title with background from the Roman Republic in classical antiquity.

don, A contraction of Middle English do on (“put on”), from Old English dōn on. Compare also doff, dup, dout. VerbEdit. don (third-person singular simple present dons,� Vijay is recruited by a police officer to masquerade as his lookalike Don, the leader of an international gang of smugglers. Things go wrong when the officer is killed and Vijay is left to fend for himself.

Comments
  • Nice solution. I will just add from myself that if you use get_posts you have to only put $query in array_column function.
  • You didn't answer the question. Your response would fit better as a comment.
  • "the_title() does not require to echo. check:" -> Correct, my bad. I didn't notice that.
  • That actually nearly works. I just had to change to get_the_title() instead. That aside, a problem has occurred I didn't expect. If I limit the posts to 10 for example, sometimes it might only return one result because of the most recent 10 posts have the same title. I'll mark your post as the correct answer anyway (I've made an edit) because it works. But if you have any suggestions for this new problem, I'm all ears.
  • It depends on the situation. Is it just the title you're querying? If you're into SQL, then instead of using WP_Query, you can create your own query with wpdb and SELECT DISTINCT post_title FROM $wpdb->posts for example... codex.wordpress.org/Class_Reference/wpdb WordPress also has a command called posts_distinct But it seems limited to certain situations, maybe if you wanted to check still codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct