I've got my custom post type 'event'. I got tne next code in single-event.php:

echo '<pre>';
echo "$post->ID\n";
echo '</pre>';

When I open my event page by normal URI /event/slug/, I got this:

array(17) {
  array(1) {
    string(13) "1439329938:36"
  array(1) {
    string(2) "36"

When I open it for preview, like /event/slug/?preview=true&preview_id=22681&preview_nonce=XXXXXXXXXXXXX , I got only this:

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

| improve this question | | | | |
  • 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

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

if (!is_array($post_meta)) { 
    $post_meta = array();
| improve this answer | | | | |
  • 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

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.

| improve this answer | | | | |

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.