I´m using a simple custom plugin that parses and saves external XML data as a serialized array in a custom field. This works fine, however, when I update the post, instead of the array I only see a message saying: "bool(false)". I guess it has to with serialize / unserialize but didn't find any clues.
I needed to serialize when I update post meta using update_post_meta($post_id, 'tb_data', serialize($new_value_array));
By omitting serialize like update_post_meta($post_id, 'tb_data', $new_value_array);
doesn't store any data in custom field. Furthermore, I have to use maybe_unserialize(get_post_meta($post->ID, 'tb_data', true));
to print the results.
There are 2 custom fields, (1) tb_item_group_id and (2) tb_data. Value of tb_data will be added using the below function,
Function I am using to update post meta is as below.
function parse_file_func($title) {
// get_tickets_array();exit;
$language = explode('-', get_bloginfo('language'));
$language = $language[0];
$file = file_get_contents('https://dl.dropboxusercontent.com/u/12344450/feed.xml');
if (!$file) {
exit;
}
$domObj = new xmlToArrayParser($file);
$domArr = $domObj->array;
if (($domObj->parse_error)) {
echo $domObj->get_xml_error();
} else {
$first = $domArr['rss']['channel']['item'];
foreach ($first as $item) {
if ($item['languageCode'] == $language) {
$args = array(
'meta_key' => 'tb_item_group_id',
'meta_value' => $item['g:item_group_id'],
'post_type' => 'tickets',
);
$post = get_posts($args);
if (empty($post)) {
continue;
} else {
$args = array(
'meta_key' => 'tb_item_group_id',
'meta_value' => $item['g:item_group_id'],
'post_type' => 'tickets',
);
$post = get_posts($args);
$post_id = $post[0]->ID;
$meta_values = get_post_meta($post_id, 'tb_data');
if (empty($meta_values)) {
$new_value_array = array();
unset($item['cdata']);
$new_value_array['tb_' . $item['g:item_group_id'] . '_' . $item['ticketID']] = $item;
}
else {
$meta_arrays = unserialize($meta_values[0]);
$new_value_array = $meta_arrays;
foreach ($meta_arrays as $meta_ticketbar => $tb_content) {
if ($meta_ticketbar == 'tb_' . $item['g:item_group_id'] . '_' . $item['ticketID']) {
unset($item['cdata']);
$new_value_array[$meta_ticketbar] = $item;
} else {
$new_value_array = $meta_arrays;
unset($item['cdata']);
$new_value_array['tb_' . $item['g:item_group_id'] . '_' . $item['ticketID']] = $item;
}
}
}
update_post_meta($post_id, 'tb_data', serialize($new_value_array));
// update_post_meta($post_id, 'tb_data', base64_encode($new_value_array));
}
} else {
continue;
}
}
}
}
and displaying on front-end using get_post_meta
<?php
// $tb_meta = get_post_meta($post->ID, 'tb_data', true);
$tb_meta = get_post_meta($post->ID, 'tb_data', true);
$tb_meta_unserialized = maybe_unserialize( $tb_meta );
?>
<pre><?php
// print_r ($tb_meta_unserialized);
var_dump($tb_meta_unserialized);
?></pre>
I have managed to fix it at my own.
I have used following code to update post meta
...and following code to get post meta
I hope this will help someone.