Home Forums Backend Issues (wp-admin) Populate Selects, but appearing duplicated Reply To: Populate Selects, but appearing duplicated

  • The problem is that you are looping over posts and then looping over the terms attached to each post so multiple posts may have the same terms.

    Question, do you really need to loop over the posts? Are you trying to only show terms that have been selected in posts?

    Assuming that the answer to those questions is yes then you need to keep track of terms you’ve already shown and not show them again

    if(have_posts()) {
      $shown_terms = array();
      while (have_posts()) {
         $terms = get_field('tipo_de_imovel');
         if ($terms) {
           foreach ($terms as $term) {
             if (!in_array($term->name, $shown_terms)) {
               $shown_terms[] = $term->name;
               echo '<option value="'.esc_html($term->name).'">'.esc_html($term->name).'</option>';
             } // end if not shown
           } // end foreach $term
         } // end if $terms
      } // end while have_posts()
    } // end if have_posts()