admin管理员组文章数量:1291102
I'm trying to fill WP SEO Structured Data Schema fields with add_post_meta function and it doesn't work properly. I checked one of the posts that's added from Wordpress admin panel and it was saved like this:
a:12:{s:6:"active";s:1:"1";s:8:"headline";s:13:"aaa";s:16:"mainEntityOfPage";s:37:"/?p=83&preview=true";s:6:"author";s:1:"c";s:5:"image";s:2:"91";s:13:"datePublished";s:1:"d";s:12:"dateModified";s:1:"e";s:9:"publisher";s:1:"f";s:14:"publisherImage";s:2:"91";s:11:"description";s:1:"g";s:11:"articleBody";s:1:"h";s:19:"alternativeHeadline";s:1:"i";}
looks like this _schema_article
is the parent of _schema_article[headline]
which I'm trying to save. this is my code:
<?php
require_once("wp-load.php");
$my_post = array(
'post_title' => "title",
'post_content' => "content",
'post_status' => 'publish',
'post_author' => 1,
);
$remote_id = wp_insert_post($my_post);
echo $remote_id;
add_post_meta($remote_id, "_schema_article[headline]", "aaa", true);
how can I create something like this JSON-like example above with add_post_meta
or other WordPress functions?
I'm trying to fill WP SEO Structured Data Schema fields with add_post_meta function and it doesn't work properly. I checked one of the posts that's added from Wordpress admin panel and it was saved like this:
a:12:{s:6:"active";s:1:"1";s:8:"headline";s:13:"aaa";s:16:"mainEntityOfPage";s:37:"http://1.test/?p=83&preview=true";s:6:"author";s:1:"c";s:5:"image";s:2:"91";s:13:"datePublished";s:1:"d";s:12:"dateModified";s:1:"e";s:9:"publisher";s:1:"f";s:14:"publisherImage";s:2:"91";s:11:"description";s:1:"g";s:11:"articleBody";s:1:"h";s:19:"alternativeHeadline";s:1:"i";}
looks like this _schema_article
is the parent of _schema_article[headline]
which I'm trying to save. this is my code:
<?php
require_once("wp-load.php");
$my_post = array(
'post_title' => "title",
'post_content' => "content",
'post_status' => 'publish',
'post_author' => 1,
);
$remote_id = wp_insert_post($my_post);
echo $remote_id;
add_post_meta($remote_id, "_schema_article[headline]", "aaa", true);
how can I create something like this JSON-like example above with add_post_meta
or other WordPress functions?
1 Answer
Reset to default 1This is the PHP serialization format, and is used to serialize arrays for storage in the database. Core WordPress functions take care of this process for you. You just need to use an array as the value.
For example, this:
$schema_article = array(
'headline' => 'aaa',
);
update_post_meta( $remote_id, '_schema_article', $schema_article );
Will save this value:
a:1:{s:8:"headline";s:3:"aaa";}
If you want to see the original array for a value, get_post_meta()
will unserialize it for you.
For example, this:
$schema_article = get_post_meta( $remote_id, '_schema_article', true );
Will return (as seen via var_dump()
):
array(1) {
["headline"]=>
string(3) "aaa"
}
So when saving the value you need to mimic the original array format. I would share what that is for your example, but it cannot be unserialized because it has been corrupted by editing the value by hand.
本文标签: post metaHow to fill custom fields with brackets in their key with addpostmeta()
版权声明:本文标题:post meta - How to fill custom fields with brackets in their key with add_post_meta()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741443821a2379075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论