douqiao4450 2017-10-03 23:19
浏览 25
已采纳

update_post_meta中的错误? 字符串更改

When i try to execute this code

update_post_meta( $id, "_woocommerce_my_meta", 'a:1:{s:4:"gtin";s:13:"10";}' );

The meta was changing to

s:27:"a:1:{s:4:"gtin";s:13:"10";}";

By simply changing the code to

update_post_meta( $id, "_woocommerce_my_meta", ':a:1:{s:4:"gtin";s:13:"10";}' );

adding a ":" at the end of string, it works...

But i dont need this ":" Is this a function bug? Or is there any reason for that? Or some way around this problem?

  • 写回答

1条回答 默认 最新

  • dsvf46980 2017-10-04 00:17
    关注

    It is not a bug. WordPress has a reason for doing this. Since, WordPress automatically serializes objects and arrays it has to differentiate between a database meta_value that is a serialization and a meta_value that really is just a string that looks like a serialization. To understand this note that update_post_meta() calls maybe_serialize()

    function maybe_serialize( $data ) {
      if ( is_array( $data ) || is_object( $data ) )
        return serialize( $data );
    
      // Double serialization is required for backward compatibility.
      // See https://core.trac.wordpress.org/ticket/12930
      // Also the world will end. See WP 3.6.1.
      if ( is_serialized( $data, false ) )
        return serialize( $data );
    
      return $data;
    }
    

    Note that if the meta value is a serialized string then it is serialized again.

    update_post_meta() and get_post_meta() automatically handles serialization and deserialization for objects and arrays. Are you sure you need to use a serialized value in your call to update_post_meta()? Note that get_post_meta() will deserialize the serialized string and return the original string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么