How to Display All Post Meta Keys and Meta Values of the Same Post ID in WordPress?

Post meta, also known as custom fields, in WordPress allows you to store additional information related to your posts. Sometimes, you might want to display all meta keys and values for a particular post.

Here’s how you can do that:

  1. Access the Meta Data: Use WordPress’s built-in get_post_meta() function. This function retrieves all meta values for a given post ID when used with an empty string as the second parameter.
$meta_data = get_post_meta( $post_id, '', true );
  1. Loop Through the Meta Data: Now, loop through the $meta_data array to display each key and value.
foreach( $meta_data as $key => $value ) {
    echo $key . ' => ' . $value[0] . '<br />';

Remember, $post_id should be replaced with the ID of the post whose meta data you want to display. The true parameter returns the meta data as a single value.

Please note, some meta keys are hidden (start with an underscore) and are used by WordPress or plugins for internal purposes. Be careful when handling these keys.

Also, make sure to sanitize the output of the meta values to prevent any security issues, especially if the data is going to be displayed in a public area of your website.

Leave a Comment