• Greetings everyone,

    I am a new developer to WordPress. I have been browsing tutorials and the forums about adding metaboxes to posts. I can do all that successfully so thanks to everyone who asks questions and replies with answers.

    To my question, in tutorials I see everyone using this:
    $meta = get_post_meta($post->ID)
    and then accessing their fields like so:
    $meta['my_custom_field']

    This does not work for me. $meta[‘my_custom_field’] will always be an array and I will need to use $meta[‘my_custom_field’][0]. Can anyone explain to me why this is happening? Are the tutorials just out of date?

    Thanks for your help

Viewing 3 replies - 1 through 3 (of 3 total)
  • The tutorials are either out of date or wrong. That’s how get_post_meta() has behaved ever since I’ve had to use it.

    If you’re working with single values, it’s normally eaiser to use get_post_meta($post->ID, "meta_key", true) to get the single value that’s set for that key.

    Thread Starter cfetr

    (@cfetr)

    Thanks, that is what I figured.

    Timothy Jacobs

    (@timothyblynjacobs)

    Not exactly true, you can still get the results as an array, like above, but it is important to note that by default the results are wrapped inside an array. just change your call to get_post_meta($post->ID, , true); which will return it without being wrapped in an array. Take a look at the codex page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘question about get_post_meta()’ is closed to new replies.