在 wordpress 中将多个元字段值保存为数组的问题

Issue on saving multiple meta fields value as array in wordpress

我创建了具有相同名称的自定义字段,在元字段中我从数据库中恢复值并希望将它们保存为我的模式中的序列化。

像这样:

Array
(
    [job] => 'sdfsdf'
    [image] => Array
        (
            [attachment_id] => '2121' 
            [url] => 'http://siteurl.com/wp-content/uploads/2016/04/image.jpg'
        )

    [popup_text] => 'A quick brown fox jumps over the lazy dog.'
)

但是当我保存这些值时,所有值都恢复为空。

现在这是我的元字段代码:

global $wpdb;

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

    $dsl_staff_qry = $wpdb->get_row("Select * From ". $wpdb->posts . " Where ID = ".$post->ID);

    $dsl_staff_parent = $wpdb->get_row("Select * From ". $wpdb->posts . " Where post_parent = " . $post->ID . " And post_type = 'attachment' And (post_mime_type = 'image/jpeg' Or post_mime_type = 'image/png')");

?>
    <table width="100%" cellspacing="2" cellpadding="2" border="0">
        <tr>
            <td width="20%">
                <strong>Job Title</strong>
            </td>
            <td width="80%">
                <input type="text" name="dsl_txt_fw_options['job']" value="<?php echo $dsl_fw_options['job'] ?>" style="width: 50%;">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="dsl_txt_fw_options['attachment_id']" value="<?php echo $dsl_staff_parent->ID; ?>" style="width: 50%;">
                <br>
                <input type="text" name="dsl_txt_fw_options['url']" value="<?php echo $dsl_staff_parent->guid; ?>" style="width: 50%;">
                <br>
                <textarea name="dsl_txt_fw_options['popup_text']"><?php echo $dsl_staff_qry->post_content; ?></textarea>
            </td>
        </tr>
    </table>

我根据需要从数据库中获取值,它完美显示。

这是我的更新元值代码:

add_action('save_post', 'dr_save_staff_post');
function dr_save_staff_post() {
    global $post;

    $dsl_arr = array();

    $dsl_arr_val = $_POST['dsl_txt_fw_options'];

    foreach ($dsl_arr_val as $dsl_key => $dsl_val) {
        $dsl_arr['job'] = $dsl_val->job;
        $dsl_arr['image'] = array(
                                'attachment_id' => $dsl_val->attachment_id,
                                'url' => $dsl_val->url
                            );
        $dsl_arr['popup_text'] = $dsl_val->popup_text;
    }

    update_post_meta($post->ID, 'fw_options', $dsl_arr);
}

当我保存这些值并检查数据库中的值时,它给出了我的结果:

a:3:{s:3:"job";N;s:5:"image";a:2:{s:13:"attachment_id";N;s:3:"url";N;}s:10:"popup_text";N;}

我不知道我哪里做错了,所以请指导我。

首先更改每个输入类型的名称。喜欢

<table width="100%" cellspacing="2" cellpadding="2" border="0">
        <tr>
            <td width="20%">
                <strong>Job Title</strong>
            </td>
            <td width="80%">
                <input type="text" name="dsl_txt_fw_job" value="<?php echo $dsl_fw_options['job ?>" style="width: 50%;">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="dsl_txt_fw_attachment_id" value="<?php echo $dsl_staff_parent->ID; ?>" style="width: 50%;">
                <br>
                <input type="text" name="dsl_txt_fw_url" value="<?php echo $dsl_staff_parent->guid; ?>" style="width: 50%;">
                <br>
                <textarea name="dsl_txt_fw_popup_text"><?php echo $dsl_staff_qry->post_content; ?></textarea>
            </td>
        </tr>
    </table>

然后获取这些值,如

$dsl_val_job = $_POST['dsl_txt_fw_job'];

$dsl_val_attachment_id = $_POST['dsl_txt_fw_attachment_id'];

$dsl_val_url = $_POST['dsl_txt_fw_url'];

$dsl_val_popup_text = $_POST['dsl_txt_fw_popup_text'];

在此之后,您可以将这些值转换为您想要的格式的数组。喜欢

$dsl_val['job'] = $dsl_val_job;

$dsl_val['image'] = array ( 'attachment_id' => $dsl_val_attachment_id, 

                             'url'=>  $dsl_val_url );

$dsl_val['popup_text'] = $dsl_val_popup_text; 

就是这样。 :-)