发布更新时出现WordPress自定义字段问题

时间:2014-12-01 15:12:29

标签: php wordpress wordpress-plugin xml-serialization

我使用的是一个简单的自定义插件,它将外部XML数据解析并保存为自定义字段中的序列化数组。这很好,但是,当我更新帖子而不是数组时,我只看到一条消息:“bool(false)”。

自定义字段的内容我试图使用序列化在WordPress Post(前端)上显示。

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&nbsp;the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005.&nbsp;<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&nbsp;the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005.&nbsp;<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&nbsp;the Conservation of Cultural Heritage and was inscribed on the UNESCO World Heritage List in 2005.&nbsp;<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:{}}}

这是我用于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;
            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

当您使用get_post_meta发布meta时,如果值已序列化,则会自动反序列化返回值。 因此,该值将被反序列化两次,您将得到错误。

$value = get_post_meta(...);

在这种情况下,如果$值是序列化数据,则$ value已经被反序列化。 所以当你unserialize($value)时,你会得到假的。 此外,您不需要在保存post meta时序列化该值,因为如果它是数组类型,update_post_meta会自动序列化它。

答案 1 :(得分:0)

我已经成功解决了这个问题。

我使用以下代码来更新post meta

update_post_meta($post_id, 'tb_data', base64_encode(serialize($new_value_array)));

...并按照以下代码获取帖子

$tb_meta = get_post_meta($post->ID, 'tb_data', true);
$tb_meta_unserialized = unserialize(base64_decode($tb_meta));

我希望这会对某人有所帮助。