将元数据发布为数组看起来像字符串,而不是数组

时间:2017-04-25 作者:Dongsan

我想在post meta中插入一个数组,但发生了一些奇怪的事情。我错过了什么?

How I insert meta to a post:

// 1st Quarter
for ($month = 1; $month <= 3; $month++) {

  $last_day = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  $first_date = strtotime("$year-$month-$first_day 00:00:00");
  $last_date = strtotime("$year-$month-$last_day 23:59:59");

  $month_dates = array(
    \'name\' => \'P\' . $month . \'-FY\' . $year,
    \'start_date\' => date(\'Y-m-d\', $first_date),
    \'end_date\' => date(\'Y-m-d\', $last_date),
    \'closed\' => 0
  );
  array_push( $tmp_period, $month_dates );

} // end for

$quarter_name = \'Q1-FY\' . $year;
add_post_meta( $post_id, \'_q1_name\', $quarter_name, true );
add_post_meta( $post_id, \'_q1_subs\', $tmp_period, false );

How I check the inserted meta:

$subs = get_post_meta( $post->ID );

echo \'<pre>\';
print_r( $subs, false );
echo \'</pre>\';

What the inserted meta actually looks like:

Array
(
    [_q1_name] => Array
        (
            [0] => Q1-FY2018
        )

    [_q1_subs] => Array
        (
            [0] => a:3:{i:0;a:4:{s:4:"name";s:9:"P1-FY2018";s:10:"start_date";s:10:"2018-01-01";s:8:"end_date";s:10:"2018-01-31";s:6:"closed";i:0;}i:1;a:4:{s:4:"name";s:9:"P2-FY2018";s:10:"start_date";s:10:"2018-02-01";s:8:"end_date";s:10:"2018-02-28";s:6:"closed";i:0;}i:2;a:4:{s:4:"name";s:9:"P3-FY2018";s:10:"start_date";s:10:"2018-03-01";s:8:"end_date";s:10:"2018-03-31";s:6:"closed";i:0;}}
        )
)

What I originally expected the meta to look like:

Array
(
    [_q1_name] => Array
        (
            [0] => Q1-FY2018
        )

    [_q1_subs] => Array
        (
            [0] => Array (
                   \'name\' => \'some name\',
                   \'start_date\' => some start date\',
                   \'end_date\' => some end date,
                   \'closed\' => some value
            ),
            [1] => Array (
                   \'name\' => \'some name\',
                   \'start_date\' => some start date\',
                   \'end_date\' => some end date,
                   \'closed\' => some value
            ),
            [2] => Array (
                   \'name\' => \'some name\',
                   \'start_date\' => some start date\',
                   \'end_date\' => some end date,
                   \'closed\' => some value
            ),
        )
)

1 个回复
最合适的回答,由SO网友:ahendwh2 整理而成

您的数据将serialized, 如果将非标量值(例如数组、对象)保存为post meta。你必须unserialize 信息技术:

$subs = get_post_meta( $post->ID );
$subs[\'_q1_subs\'][0] = unserialize($subs[\'_q1_subs\'][0]);

相关推荐

警告:CALL_USER_FUNC_ARRAY()要求参数1是有效的回调

我创建了一个新的wordpress,没有主题,只有一个插件:GDPR WP。(我想先在空wordpress上试用这个插件,然后再将它部署到其他网站上)。所以,我第一次尝试创建函数来接受或不接受GoogleAnalytics cookie。我的函数工作正常,但在BO/FO上有一个错误:Warning: call_user_func_array() expects parameter 1 to be a valid callback, function \'cookie_GA\' not found or i