Remove duplicated values from a loop

I don’t know of any WordPress-specific way to exclude duplicate-title posts from a query and I’m sure that there are multiple ways to address this. The solution that jumps to mind is to store each post title in an array and then check to see if that title is already there before outputting the post.

Some quick untested code:

<?php
    $args = array(
      'post_type'     => 'packs',
      'order'         => 'ASC',
      'orderby'       => 'title'
    );
    $the_query = new WP_Query( $args );
    // array to store cities
    $unique_cities = array();
    if( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
        $city = get_the_field('departures');
        // only create option if city hasn't been added yet
        if( ! in_array( $city, $unique_cities ) ) :
            // add city to array so it doesn't repeat
            $unique_cities[] = $city;
    ?>

            <option value="<?php echo $city; ?>"><?php echo $city; ?></option>

  <?php
    endif;
  endwhile; endif; ?>

Also, note two other minor changes that I think you intended:

  • added orderby parameter to WP_Query so that cities are listed A-Z.
  • fixed first have_posts() so it’s testing your custom query