admin管理员组文章数量:1122846
I've got my custom post type 'event'. I got tne next code in single-event.php:
get_header();
the_post();
echo '<pre>';
echo "$post->ID\n";
var_dump(get_post_meta($post->ID));
echo '</pre>';
When I open my event page by normal URI /event/slug/, I got this:
22681
array(17) {
["_edit_lock"]=>
array(1) {
[0]=>
string(13) "1439329938:36"
}
["_edit_last"]=>
array(1) {
[0]=>
string(2) "36"
}
...
}
When I open it for preview, like /event/slug/?preview=true&preview_id=22681&preview_nonce=XXXXXXXXXXXXX , I got only this:
22681
string(0) ""
(where first line is a correct post ID and is the same as preview_id param)
What am I doing wrong? And why string(0)
, and not array(0)
?
I've got my custom post type 'event'. I got tne next code in single-event.php:
get_header();
the_post();
echo '<pre>';
echo "$post->ID\n";
var_dump(get_post_meta($post->ID));
echo '</pre>';
When I open my event page by normal URI /event/slug/, I got this:
22681
array(17) {
["_edit_lock"]=>
array(1) {
[0]=>
string(13) "1439329938:36"
}
["_edit_last"]=>
array(1) {
[0]=>
string(2) "36"
}
...
}
When I open it for preview, like /event/slug/?preview=true&preview_id=22681&preview_nonce=XXXXXXXXXXXXX , I got only this:
22681
string(0) ""
(where first line is a correct post ID and is the same as preview_id param)
What am I doing wrong? And why string(0)
, and not array(0)
?
2 Answers
Reset to default 0I solved this problem with 3 lines of code after calling get_post_meta():
if (!is_array($post_meta)) {
$post_meta = array();
}
The answer to your question why it returns an empty string is listed in the comment of the get_post_meta()
entry in the WP Developers docs: https://developer.wordpress.org/reference/functions/get_post_meta/#comment-825
The function requires the following:
get_post_meta( $postID, 'meta_key', $single=false );
As the comment of the link above explains:
If a meta field with the given $key
isn’t found for the given $post_id
, the return value varies:
If $single
is true
, an empty string is returned.
If $single
is false
, an empty array is returned.
$single
defaults to false
and no meta_key
has been given, therefore an empty array is returned.
本文标签: getpostmeta(postgtID) returns empty string when in preview mode of custom post type
版权声明:本文标题:get_post_meta($post->ID) returns empty string when in preview mode of custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293458a1929151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
get_post_meta
returns essentially nothing - no underscored fields and no regular fields when called from preview. – SergeAx Commented Aug 22, 2015 at 13:06