1

Hi I bought a theme but I need to add some fields to a frontend submission form. I added the metaboxes and they are working but the problem is the select type of metaboxes.

I cloned a select form group that gets the data from the post taxonomies but I don't know how to tweak it to make it "grab" the data from the 'items' array.

Here's my metabox:

array(
    'type'  => 'select',
    'name'  => 'resume_change_location',
    'label' => __( 'Are you willing to change location?', 'jobboard' ),
    'description'   => __( '' ),
    'items' => array(
        array(
            'value' => 'yes',
            'label' => __( 'Yes', 'jobboard' ),
        ),
        array(
            'value' => 'no',
            'label' => __( 'No', 'jobboard' ),
        ),
    ),

),

Here's the dropdown for the taxonomy data (working):

<select name="job_type" id="job_type" class="form-control">
                            <option value=""><?php echo '-- '.__( 'Select Type', 'jobboard' ).' --'; ?></option>
                        <?php
                            $terms = get_terms( 'job_type', array( 'hide_empty' => false, ) );
                            foreach( $terms as $term ){
                                $selected = '';
                                if( $default['job_type'] == $term->slug ){
                                    $selected = 'selected';
                                }
                                echo '<option value="'.$term->slug.'" '.$selected.'>'.esc_attr($term->name).'</option>';
                            }
                        ?>
                        </select>

I would like to show the dropdown with the items in my metabox...here's what I got but it is not showing my items (it is showing the dropdown but with blank options)

<select class="form-control" name="change">
                        <option value=""><?php _e( 'Select', 'jobboard' ); ?></option>
                    <?php
                        $change = get_post_meta($post->ID, $key, 'resume_change_location', true);
                        foreach( $change as $key){
                            $selected = '';
                            if( $key->name == $default['resume_change_location'] ){
                                $selected = 'selected="selected"';
                            }//endif;
                            echo '<option value="'.$key->name.'" '.$selected.'>'.esc_attr($key->name).'</option>';
                        }
                    ?>
                    </select>   

I have been looking for a way to make this work for about 3 hours with no luck... Any help is highly appreciated.

1 Answer 1

1
$change = get_post_meta($post->ID, $key, 'resume_change_location', true);

From https://developer.wordpress.org/reference/functions/get_post_meta/

get_post_meta returns an array, not an object so you need to use $change['index']

The other thing is within your meta box you seem to be saving a string not an array so foreach is going to fail.

The below should work...

    $change = get_post_meta( $post->ID, $key, 'resume_change_location', true );
    <option value=""><?php _e( 'Select', 'jobboard' ); ?></option>
    <option value="Yes"<?php selected( 'Yes', $default['resume_change_location'] ); ?>>Yes</option>
    <option value="No"<?php selected( 'No', $default['resume_change_location'] ); ?>>No</option>
    </select>

Also notice my use of the built-in WordPress selected function - https://codex.wordpress.org/Function_Reference/selected

1
  • Thanks the values are now showing. I think I forgot to say that I need the front end and backend to be working (can edit via frontend or backend). I think this is working frontend, but how can I "bond" the options to the backend too? Thanks!! Aug 19, 2015 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.