Hello,
I’ve noticed that it’s a quite popular topic yet, unfortunately – none of the found solutions are not working for me.
What I’m trying to achieve is to simply show a dropdown with labels and values based on a repeater subfield. I’ve managed to create such a piece of code:
function fnc_wybierz_wojewodztwo() {
$args = array(
'post_type' => 'opencourses',
'meta_key' => 'terminy_warsztatow'
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()): ?>
<select type="text" class="form-control filter-select" name="lokalizacja" id="lokalizacja"> <?php
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('terminy_warsztatow')):
while (have_rows('terminy_warsztatow')) : the_row();
$field = get_sub_field_object('wojewodztwo');
if( $field['choices'] ): ?>
<?php foreach( $field['choices'] as $value => $label ): ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach;
endif;
endwhile;
else :
echo 'sorry, we didnt find anything'; // no rows found
endif;
endwhile; ?>
</select>
<?php endif; wp_reset_postdata();
}
add_shortcode( 'wybierz_wojewodztwo', 'fnc_wybierz_wojewodztwo' );
I struggle only with one thing – the dropdown provides endless/infinite number of options. I have no clue where the issue within code is. I’ve reset the loop, what else i can do?
If you are trying to create a select field that matches the choices of the field in questions then you don’t need to get every post and loop over them, so there shouldn’t be a need for the query, just use get_field_object()
using the field key
$field = get_field_object('field_XXXXXXX');
foreach ($field['choices'] as $value => $label) ..... etc