0

I want to insert an array into a post meta, but something strange is happening. What am I missing here?

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
            ),
        )
)
2

Your data will be serialized, if you save non-scalar values (e.g. array, object) as post meta. You must unserialize it:

$subs = get_post_meta( $post->ID );
$subs['_q1_subs'][0] = unserialize($subs['_q1_subs'][0]);
  • Is there a way that I can unserialize it before I add it as post meta? – Dongsan Apr 25 '17 at 6:49
  • The data will be serialized when saving to the database, because you cannot save unserialized data. So it must be serialized while adding it as post meta. You must unserialize it after getting the post meta. – ahendwh2 Apr 25 '17 at 8:32
  • Got it...didn't know Wordpress serializes it by design. Thanks man! – Dongsan Apr 25 '17 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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