admin管理员组文章数量:1306985
I insert this and it works fine:
add_post_meta( get_the_ID(), 'robots', 'TEXT', true );
After adding that, I check the database and everything is fine, the result is TEXT.
But if I insert this:
add_post_meta( get_the_ID(), 'robots', 'a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}', true );
The result is:
s:43:"a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}";
And I need the result to be exactly the same as I have added it:
a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}
I need not to automatically add that s:43:" and the "; at the end.
Can anybody help me please?
I insert this and it works fine:
add_post_meta( get_the_ID(), 'robots', 'TEXT', true );
After adding that, I check the database and everything is fine, the result is TEXT.
But if I insert this:
add_post_meta( get_the_ID(), 'robots', 'a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}', true );
The result is:
s:43:"a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}";
And I need the result to be exactly the same as I have added it:
a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}
I need not to automatically add that s:43:" and the "; at the end.
Can anybody help me please?
Share Improve this question edited Jan 26, 2021 at 0:36 Sally CJ 40.1k2 gold badges28 silver badges49 bronze badges asked Jan 25, 2021 at 23:36 Carlos BCarlos B 133 bronze badges 3- 1 I have solved it using unserialize(). Example: add_post_meta( get_the_ID(), 'robots', unserialize('a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}'), true ); – Carlos B Commented Jan 26, 2021 at 0:15
- Does my revised answer answer your question? If so, I'd appreciate if you can mark my answer as "correct" - that way, it'll be clear to others that the question has an accepted solution. Or maybe, just +1 (upvote) my answer? :) – Sally CJ Commented Jan 27, 2021 at 3:35
- 1 Sorry @SallyCJ I don't have much experience in stackexchange, but I have already marked your answer as correct :) Best regards. – Carlos B Commented Jan 27, 2021 at 16:45
1 Answer
Reset to default 1Why the content is not being respected
And it's not just add_post_meta()
, but any functions like add_term_meta()
and add_user_meta()
that uses add_metadata()
.
And the reason why the value gets serialized again is because add_metadata()
will automatically serialize the meta value using maybe_serialize()
which (unfortunately) will serialize the value even if it's already serialized.
How to tackle the issue
In your case, if possible, pass an array instead of the serialized value/string:
// A non-scalar value will be automatically serialized by maybe_serialize().
$value = array( 'noindex', 'nofollow' );
add_post_meta( get_the_ID(), 'robots', $value, true );
However, if you retrieved/received the value from somewhere and you need to retain its exact format when saving, then — as you've already figured it out — you'll need to unserialize the value before passing it to add_post_meta()
to prevent the double serialization.
But in WordPress, instead of using the native unserialize()
function in PHP, I suggest you to use maybe_unserialize()
instead:
$value = 'a:2:{i:0;s:7:"noindex";i:1;s:8:"nofollow";}';
// Unserialize the value and pass it to add_post_meta().
add_post_meta( get_the_ID(), 'robots', maybe_unserialize( $value ), true );
本文标签: mysqladdpostmeta does not respect the content
版权声明:本文标题:mysql - add_post_meta does not respect the content 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741801345a2398243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论