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)".
Content of custom field I am trying to display on WordPress Post (front end) using serialization.
a:3:{s:8:"tb_88_40";a:20:{s:4:"guid";s:36:"26e4a3f6-afc4-457a-b34f-c09c3f4cfb1b";s:5:"title";s:11:"Casa Batllo";s:8:"language";s:1:"4";s:12:"languageCode";s:2:"en";s:8:"ticketID";s:2:"40";s:6:"ticket";s:5:"Adult";s:4:"link";s:54:"http://barcelona.ticketbar.eu/en/museums/casa-batllo-/";s:11:"description";s:3085:"
<h3><em>Architecture of Antoni Gaudi in Barcelona</em></h3>
<p align="justify"><span id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null" closure_uid_3d5c4l="95">The colorful</span> <span class="hps" tc="null" closure_uid_3d5c4l="96">Casa</span> <span class="hps" tc="null" closure_uid_3d5c4l="97">Batlló</span><span tc="null" closure_uid_3d5c4l="98">,</span> <span class="hps" tc="null" closure_uid_3d5c4l="99">a</span> <span class="hps" tc="null" closure_uid_3d5c4l="100">house from the 19th</span> <span class="hps" tc="null" closure_uid_3d5c4l="101">century</span> <span class="hps" tc="null" closure_uid_3d5c4l="102">renovated in the well-known</span> <span class="hps" tc="null" closure_uid_3d5c4l="104">modernist</span> <span class="hps" tc="null" closure_uid_3d5c4l="105">style</span><span class="hps" tc="null" closure_uid_3d5c4l="106">, is</span> <span class="hps" tc="null" closure_uid_3d5c4l="107">one of many</span> <span class="hps" tc="null" closure_uid_3d5c4l="108">masterpieces</span> <span class="hps"
tc="null" closure_uid_3d5c4l="109">of Gaudí</span> <span class="hps" tc="null" closure_uid_3d5c4l="110">in Barcelona</span><span tc="null" closure_uid_3d5c4l="111">.</span> <span class="hps" tc="null" closure_uid_3d5c4l="112">The interior</span> <span class="hps" tc="null" closure_uid_3d5c4l="113">of the house</span> <span class="hps" tc="null" closure_uid_3d5c4l="117">is</span> <span class="hps" tc="null" closure_uid_3d5c4l="118">just as exceptional</span> <span class="hps" tc="null" closure_uid_3d5c4l="119">as the exterior</span> <span class="hps" tc="null" closure_uid_3d5c4l="120">of the building.</span></span> The building has become a famous landmark in Barcelona and is open for the public. The Casa Batlló was awarded the 2004 Europa Nostra Award for the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005. <span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null"
closure_uid_3d5c4l="256">Undoubtedly</span> <span class="hps" tc="null" closure_uid_3d5c4l="257">worth a visit</span><span tc="null" closure_uid_3d5c4l="258">.</span></span></p>
<p align="justify"><span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span tc="null" closure_uid_3d5c4l="258"><em>Audioguide included available in: Spanish, English, French, Italian, German, Chinese,Japanese, Portuguese, Russian, and Catalan</em><br />
</span></span></p>
<p align="justify"><em><strong>Opening Hours</strong><br />
</em> Monday - Sunday from 9 a.m to 9 p.m (Last entrance at 8 p.m.)<br />
<em><br />
<strong>Location</strong><br />
</em> Passeig de Gracia 43<br />
08007 Barcelona<br />
Metro to Diagonal or Passeig de Gracia</p>
<em><strong>Ticket is valid for 3 months from date of purchase! Buy your ticket online and skip the line!</strong></em><br />
<br />
<br />
";s:12:"g:image_link";s:95:"http://barcelona.ticketbar.eu/barcelona/classificaties/museums/casa-batllo/casa-batllo_grid.jpg";s:8:"oldPrice";a:0:{}s:14:"disabled_dates";s:13:"
";s:9:"day_parts";s:13:"
";s:18:"visit_day_required";s:32:"
false
";s:7:"g:price";s:9:"EUR 21.50";s:11:"g:condition";s:3:"new";s:15:"g:item_group_id";s:2:"88";s:4:"g:id";s:6:"135218";s:6:"rating";a:0:{}s:16:"extraInformation";s:1:"i";s:15:"customerService";a:0:{}}s:9:"tb_88_704";a:20:{s:4:"guid";s:36:"26e4a3f6-afc4-457a-b34f-c09c3f4cfb1b";s:5:"title";s:11:"Casa Batllo";s:8:"language";s:1:"4";s:12:"languageCode";s:2:"en";s:8:"ticketID";s:3:"704";s:6:"ticket";s:11:"Child (0-6)";s:4:"link";s:54:"http://barcelona.ticketbar.eu/en/museums/casa-batllo-/";s:11:"description";s:3085:"
<h3><em>Architecture of Antoni Gaudi in Barcelona</em></h3>
<p align="justify"><span id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null" closure_uid_3d5c4l="95">The colorful</span> <span class="hps" tc="null" closure_uid_3d5c4l="96">Casa</span> <span class="hps" tc="null" closure_uid_3d5c4l="97">Batlló</span><span tc="null" closure_uid_3d5c4l="98">,</span> <span class="hps" tc="null" closure_uid_3d5c4l="99">a</span> <span class="hps" tc="null" closure_uid_3d5c4l="100">house from the 19th</span> <span class="hps" tc="null" closure_uid_3d5c4l="101">century</span> <span class="hps" tc="null" closure_uid_3d5c4l="102">renovated in the well-known</span> <span class="hps" tc="null" closure_uid_3d5c4l="104">modernist</span> <span class="hps" tc="null" closure_uid_3d5c4l="105">style</span><span class="hps" tc="null" closure_uid_3d5c4l="106">, is</span> <span class="hps" tc="null" closure_uid_3d5c4l="107">one of many</span> <span class="hps" tc="null" closure_uid_3d5c4l="108">masterpieces</span> <span class="hps"
tc="null" closure_uid_3d5c4l="109">of Gaudí</span> <span class="hps" tc="null" closure_uid_3d5c4l="110">in Barcelona</span><span tc="null" closure_uid_3d5c4l="111">.</span> <span class="hps" tc="null" closure_uid_3d5c4l="112">The interior</span> <span class="hps" tc="null" closure_uid_3d5c4l="113">of the house</span> <span class="hps" tc="null" closure_uid_3d5c4l="117">is</span> <span class="hps" tc="null" closure_uid_3d5c4l="118">just as exceptional</span> <span class="hps" tc="null" closure_uid_3d5c4l="119">as the exterior</span> <span class="hps" tc="null" closure_uid_3d5c4l="120">of the building.</span></span> The building has become a famous landmark in Barcelona and is open for the public. The Casa Batlló was awarded the 2004 Europa Nostra Award for the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005. <span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null"
closure_uid_3d5c4l="256">Undoubtedly</span> <span class="hps" tc="null" closure_uid_3d5c4l="257">worth a visit</span><span tc="null" closure_uid_3d5c4l="258">.</span></span></p>
<p align="justify"><span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span tc="null" closure_uid_3d5c4l="258"><em>Audioguide included available in: Spanish, English, French, Italian, German, Chinese,Japanese, Portuguese, Russian, and Catalan</em><br />
</span></span></p>
<p align="justify"><em><strong>Opening Hours</strong><br />
</em> Monday - Sunday from 9 a.m to 9 p.m (Last entrance at 8 p.m.)<br />
<em><br />
<strong>Location</strong><br />
</em> Passeig de Gracia 43<br />
08007 Barcelona<br />
Metro to Diagonal or Passeig de Gracia</p>
<em><strong>Ticket is valid for 3 months from date of purchase! Buy your ticket online and skip the line!</strong></em><br />
<br />
<br />
";s:12:"g:image_link";s:95:"http://barcelona.ticketbar.eu/barcelona/classificaties/museums/casa-batllo/casa-batllo_grid.jpg";s:8:"oldPrice";a:0:{}s:14:"disabled_dates";s:13:"
";s:9:"day_parts";s:13:"
";s:18:"visit_day_required";s:32:"
false
";s:7:"g:price";s:8:"EUR 0.00";s:11:"g:condition";s:3:"new";s:15:"g:item_group_id";s:2:"88";s:4:"g:id";s:6:"135219";s:6:"rating";a:0:{}s:16:"extraInformation";s:1:"i";s:15:"customerService";a:0:{}}s:10:"tb_88_1310";a:20:{s:4:"guid";s:36:"26e4a3f6-afc4-457a-b34f-c09c3f4cfb1b";s:5:"title";s:11:"Casa Batllo";s:8:"language";s:1:"4";s:12:"languageCode";s:2:"en";s:8:"ticketID";s:4:"1310";s:6:"ticket";s:12:"Child (7-17)";s:4:"link";s:54:"http://barcelona.ticketbar.eu/en/museums/casa-batllo-/";s:11:"description";s:3085:"
<h3><em>Architecture of Antoni Gaudi in Barcelona</em></h3>
<p align="justify"><span id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null" closure_uid_3d5c4l="95">The colorful</span> <span class="hps" tc="null" closure_uid_3d5c4l="96">Casa</span> <span class="hps" tc="null" closure_uid_3d5c4l="97">Batlló</span><span tc="null" closure_uid_3d5c4l="98">,</span> <span class="hps" tc="null" closure_uid_3d5c4l="99">a</span> <span class="hps" tc="null" closure_uid_3d5c4l="100">house from the 19th</span> <span class="hps" tc="null" closure_uid_3d5c4l="101">century</span> <span class="hps" tc="null" closure_uid_3d5c4l="102">renovated in the well-known</span> <span class="hps" tc="null" closure_uid_3d5c4l="104">modernist</span> <span class="hps" tc="null" closure_uid_3d5c4l="105">style</span><span class="hps" tc="null" closure_uid_3d5c4l="106">, is</span> <span class="hps" tc="null" closure_uid_3d5c4l="107">one of many</span> <span class="hps" tc="null" closure_uid_3d5c4l="108">masterpieces</span> <span class="hps"
tc="null" closure_uid_3d5c4l="109">of Gaudí</span> <span class="hps" tc="null" closure_uid_3d5c4l="110">in Barcelona</span><span tc="null" closure_uid_3d5c4l="111">.</span> <span class="hps" tc="null" closure_uid_3d5c4l="112">The interior</span> <span class="hps" tc="null" closure_uid_3d5c4l="113">of the house</span> <span class="hps" tc="null" closure_uid_3d5c4l="117">is</span> <span class="hps" tc="null" closure_uid_3d5c4l="118">just as exceptional</span> <span class="hps" tc="null" closure_uid_3d5c4l="119">as the exterior</span> <span class="hps" tc="null" closure_uid_3d5c4l="120">of the building.</span></span> The building has become a famous landmark in Barcelona and is open for the public. The Casa Batlló was awarded the 2004 Europa Nostra Award for the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005. <span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span class="hps" tc="null"
closure_uid_3d5c4l="256">Undoubtedly</span> <span class="hps" tc="null" closure_uid_3d5c4l="257">worth a visit</span><span tc="null" closure_uid_3d5c4l="258">.</span></span></p>
<p align="justify"><span class="short_text" id="result_box" tc="null" a="undefined" c="4" closure_uid_3d5c4l="129" lang="en"><span tc="null" closure_uid_3d5c4l="258"><em>Audioguide included available in: Spanish, English, French, Italian, German, Chinese,Japanese, Portuguese, Russian, and Catalan</em><br />
</span></span></p>
<p align="justify"><em><strong>Opening Hours</strong><br />
</em> Monday - Sunday from 9 a.m to 9 p.m (Last entrance at 8 p.m.)<br />
<em><br />
<strong>Location</strong><br />
</em> Passeig de Gracia 43<br />
08007 Barcelona<br />
Metro to Diagonal or Passeig de Gracia</p>
<em><strong>Ticket is valid for 3 months from date of purchase! Buy your ticket online and skip the line!</strong></em><br />
<br />
<br />
";s:12:"g:image_link";s:95:"http://barcelona.ticketbar.eu/barcelona/classificaties/museums/casa-batllo/casa-batllo_grid.jpg";s:8:"oldPrice";a:0:{}s:14:"disabled_dates";s:13:"
";s:9:"day_parts";s:13:"
";s:18:"visit_day_required";s:32:"
false
";s:7:"g:price";s:9:"EUR 18.50";s:11:"g:condition";s:3:"new";s:15:"g:item_group_id";s:2:"88";s:4:"g:id";s:6:"135220";s:6:"rating";a:0:{}s:16:"extraInformation";s:1:"i";s:15:"customerService";a:0:{}}}
Here is the function I am using for update_post_meta
.
function parse_file_func($title) {
// get_tickets_array();exit;
$language = explode('-', get_bloginfo('language'));
$language = $language[0];
$file = file_get_contents('http://vendor.ticketbar.eu/feed/284/');
// $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;
}
}
}
}
When you get post meta using get_post_meta
, the return value is unserialized automatically if the value was serialized. So the value will be unserialized two times and you will get false.
$value = get_post_meta(...);
In this case, the $value is already unserialized if it was the serialized data. So when you unserialize($value)
, you will get false. Also you don't need to serialize the value when saving post meta because the update_post_meta
serialize it automatically if it is array type.
I have managed to fix it at my own.
I have used following code to update post meta
update_post_meta($post_id, 'tb_data', base64_encode(serialize($new_value_array)));
...and following code to get post meta
$tb_meta = get_post_meta($post->ID, 'tb_data', true);
$tb_meta_unserialized = unserialize(base64_decode($tb_meta));
I hope this will help someone.