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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?