1

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)?

  • Hi, @mayeenul-islam! Thank you for your reply, but it is not what I meant. What I have is get_post_meta returns essentially nothing - no underscored fields and no regular fields when called from preview. – SergeAx Aug 22 '15 at 13:06
0

I solved this problem with 3 lines of code after calling get_post_meta():

if (!is_array($post_meta)) { 
    $post_meta = array();
}
  • 1
    $post_meta = (array) $post_meta; would do what you need. – fuxia May 9 '16 at 10:08
  • Hi, тёзка :) Thank you for you answer. Alas, you solved another problem. My problem was that of get_post_meta returns different values in preview and normal modes. – SergeAx May 12 '16 at 16:28
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.