หลายเลือกปัญหาการประหยัดมูลค่า

ฉันสร้างเมตาบ็อกซ์และในนี้ฉันจะเลือกตัวเลือกที่มีหลายรายการ

$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>

เมื่อฉันเลือกหลายตัวเลือก มันจะบันทึกเพียงตัวเลือกเดียว ฉันต้องการบันทึกตัวเลือกที่เลือก

มีข้อเสนอแนะใด ๆ

กำลังบันทึกค่าเมตา

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

person Community    schedule 12.12.2013    source แหล่งที่มา


คำตอบ (2)


$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>



หากต้องการรับค่าที่เลือกหลายค่าขณะจัดเก็บ ให้ทำดังนี้:

$opt_meta_author = serialize($_POST['opt_meta_author']);
person Parixit    schedule 12.12.2013
comment
ไม่มีอะไรเกิดขึ้น ให้ฉันได้แค่นี้ - s:6:Nadeem; - person ; 12.12.2013
comment
@deemi.exe หากคุณเลือกหลายรายการ มันจะถูกส่งเป็นอาร์เรย์ไปยังเซิร์ฟเวอร์ และคุณต้องทำให้เป็นอนุกรมก่อนบันทึกลงฐานข้อมูล - person Parixit; 12.12.2013
comment
ฉันทำสิ่งนี้ $opt_meta_author = serialize($_POST['opt_meta_author']); update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author); - person ; 12.12.2013
comment
เมื่อฉันบันทึกด้วยค่าที่เลือกหลายค่า มันจะให้ค่าเดียวเท่านั้น - person ; 12.12.2013

var dump ของ $_POST['opt_meta_author']) คืออะไร? หากเป็นอาร์เรย์ ให้แปลงเป็นสตริงโดยใช้ implode และบันทึกสตริงในฐานข้อมูล

person baig    schedule 12.12.2013
comment
นี่คือสตริง คุณช่วยบอกวิธีบันทึกใน db หน่อยได้ไหม - person ; 12.12.2013
comment
ฉันเดาว่า update_post_meta( $post->ID, 'opt_meta_author', $opt_meta_author); ควรทำงานเกี่ยวกับการบันทึกในฐานข้อมูล - person baig; 12.12.2013
comment
ขอบคุณมากที่ฉันใช้ implode และการทำงานของมัน แต่ข้อผิดพลาดอย่างหนึ่งที่ฉันทำคือ ‹select name=opt-meta_author› และทำสิ่งนี้ ‹select name=opt-meta_author[]› และมันทำงานได้ดีกับ implode() - person ; 12.12.2013