admin管理员组文章数量:1303068
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('.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'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/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>
Share
Improve this question
edited Jan 23, 2018 at 18:54
Nathan Johnson
6,5286 gold badges30 silver badges49 bronze badges
asked Dec 2, 2014 at 11:53
Dipak G.Dipak G.
2282 silver badges9 bronze badges
2 Answers
Reset to default 3I 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.
You definitely don't need base64_encode
/base64_dencode
for doing PHP serialization/unserialization.
If the metadata contains a serialized array, it will be automatically processed by unserialize
function. So, to get unserialized data you do not need to use unserialize
nor maybe_unserialize
function on received data when using get_post_meta
.
Also, there is no need for using serialize
when using add_post_meta
or update_post_meta
; if we pass something other than a primitive and non-structural data type like an array or an object, then that will be detected and serialized. So, we only useed to serialize the data when we want to explicit serialize primitive data types like strings, integers, etc.
本文标签: custom fieldgetpostmeta() unserialize issuereturns boolean(false)
版权声明:本文标题:custom field - get_post_meta() unserialize issue - returns boolean(false) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741738124a2395153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论