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()) {
the_post();
$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()
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.