Skip to content

Instantly share code, notes, and snippets.

@warderer
Created October 5, 2021 06:47
Show Gist options
  • Save warderer/5d74702156f9c38d671f440e7778d494 to your computer and use it in GitHub Desktop.
Save warderer/5d74702156f9c38d671f440e7778d494 to your computer and use it in GitHub Desktop.
[Show All Post Meta Keys and Values for a Post in WordPress] If you wanted to see all the post meta keys and values for a post,page or custom post type in WordPress you can either see them in the database in the wp_postmeta table or you could use the get_post_meta function to retrieve all the post meta or a specific key. #wordpress #cpt #get_pos…
/*
If you wanted to see all the post meta keys and values for a post,page or custom post type in WordPress you can either see them in the database in the wp_postmeta table or you could use the get_post_meta function to retrieve all the post meta or a specific key.
To output the data to the head of the page you can just hook the output to wp_head.
You can also use this code in a shortcode inside a query.
Source: https://wpbeaches.com/show-all-post-meta-keys-and-values-for-a-post-in-wordpress/
*/
add_action('wp_head', 'output_all_postmeta' );
function output_all_postmeta() {
$postmetas = get_post_meta(get_the_ID());
foreach($postmetas as $meta_key=>$meta_value) {
echo $meta_key . ' : ' . $meta_value[0] . '<br/>';
}
}
@donnamcmaster
Copy link

Note that if there are multiple entries for a single meta_key, this function will output only the first one found, so it is not actually outputting "all post meta" as the function name implies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment