Home Forums Front-end Issues Select not displaying labels correctly


Select not displaying labels correctly

  • Hello! I’m making a planner with advanced custom fields, and once logged in, the admin users can add rows of a repeater group in a page from the frontend. I made this code but the select isn’t displaying the labels, only the values. Can somebody find the issue? I tried so many things already…

                    // get the repeater field key and the post ID
                    $repeater_key = 'tareas'; // replace with your field key
                    $post_id = 19744; // replace with your specific post ID
                    // get the existing rows of the repeater field for the specific post ID
                    $existing_rows = get_field( $repeater_key, $post_id );
                    // get the options for the select field from another field within the same repeater group
                    $select_options = array();
                    foreach( $existing_rows as $row ) {
                        $select_option = $row['categoria'];
                        $option_value = $select_option['value'];
                        $option_label = $select_option['choices'][ $value ];
                        if( !empty( $select_option ) ) {
                            $select_options[$select_option] = $select_option;
                    // check if form has been submitted
                    if( isset( $_POST['submit'] ) ) {
                        // check if all fields have been filled in
                        if( !empty( $_POST['titulo'] ) && !empty( $_POST['fecha'] ) && !empty( $_POST['categoria'] ) ) {
                            // create a new row with the submitted data
                            $new_row = array(
                                'titulo' => $_POST['titulo'],
                                'fecha' => $_POST['fecha'],
                                // add additional sub fields as needed
                            // add the select field to the new row
                            $new_row['categoria'] = $_POST['categoria'];
                            // add the new row to the existing rows
                            $existing_rows[] = $new_row;
                            // update the repeater field with the new rows for the specific post ID
                            update_field( $repeater_key, $existing_rows, $post_id );
                            // redirect to the same page to prevent form resubmission
                            wp_redirect( home_url('/planner') );
                        } else {
                            // display error message if not all fields have been filled in
                            $error_message = "Para añadir esta tarea, todos los campos se tienen que rellenar completamente.";
                    <!-- display the form to add a new row -->
                    <form method="POST">
                        <input type="text" name="titulo" placeholder="Título">
                        <input type="date" name="fecha" placeholder="dd-mm-yyyy">
                        <select name="categoria">
                            <?php foreach( $select_options as $option_value => $option_label ) : ?>
                                <option value="<?php echo $option_value; ?>"><?php echo $option_label; ?></option>
                            <?php endforeach; ?>
                        <!-- add additional sub fields as needed -->
                        <input type="submit" name="submit" value="Añadir tarea">

    Thank you!

  • this

    $select_option = $row['categoria'];

    will return the value of that row base on what the return values is set to. This can be the value, the label or both.

    This will only be set if you are returning both the value and the label

    $option_value = $select_option['value'];

    This ha no values because you are getting the field value and not the field object. ['choices'] is only available when getting the field object,

    $option_label = $select_option['choices'][ $value ];

    if you are returning the value and the label then the label will be in

    $option_value = $select_option['label'];
  • Hello,

    The problem was the select fields weren’t configured to return both value and label (array), that’s was the silly issue.

    Thank you.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.