user2967872
user2967872

Reputation:

Multiple Select value saving Issue

i make meta box and in this i make select option with multiple

$opt_meta_author = get_post_meta($post->ID, 'opt_meta_author', true);

<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
    <?php
        $auth_args = array(
            'post_type' => 'author',
            'orderby' => 'title',
            'order' => 'ASC'
        );

        $authors = new WP_Query($auth_args);

        while($authors->have_posts()) :
            $authors->the_post();
    ?>
        <option value="<?php echo the_title() ?>">
            <?php echo the_title() ?>
        </option>
    <?php
        endwhile;
    ?>
</select>

when i select multiple options it save only one option, i want to save selected options

is there any suggestions

Saving meta values

$opt_meta_author = $_POST['opt_meta_author'];
update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author);

Upvotes: 1

Views: 654

Answers (2)

user2704289
user2704289

Reputation:

What is the var dump of $_POST['opt_meta_author'])? if it is an array, convert it in string using implode and save the string in database

Upvotes: 1

Parixit
Parixit

Reputation: 3855

$opt_meta_author = unserialize(get_post_meta($post->ID, 'opt_meta_author', true));

<select name="opt_meta_author" id="opt_meta_author" multiple="multiple">
                <?php
                    $auth_args = array(
                        'post_type' => 'author',
                        'orderby' => 'title',
                        'order' => 'ASC'
                    );
                $authors = new WP_Query($auth_args);

                while($authors->have_posts()) :
                    $authors->the_post();
            ?>
                    <option value="<?php echo the_title() ?>">
                        <?php echo the_title() ?>
                    </option>
            <?php
                endwhile;
            ?>
            </select>



To get multiple selected value while storing do this:

$opt_meta_author = serialize($_POST['opt_meta_author']);

Upvotes: 1

Related Questions