Skip to content

Instantly share code, notes, and snippets.

@adamcrampton
Created April 8, 2019 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrampton/f5c933409c0ec09a513d24ef5cf279e4 to your computer and use it in GitHub Desktop.
Save adamcrampton/f5c933409c0ec09a513d24ef5cf279e4 to your computer and use it in GitHub Desktop.
WordPress - Return all post meta values in flat array
<?php
// Return all meta for a single post in a flat array.
function get_all_post_meta($post_id) {
// Get all the meta values.
$meta = get_post_meta($post_id, '');
// We only need the first item - return a flat array.
return array_map(function($item) {
return $item[0];
}, $meta);
}
@kostikovmu
Copy link

I added the code to remove service fields, like "_edit_lock":
`<?php
function get_all_post_meta($post_id) {
// Get all the meta values.
$meta = get_post_meta($post_id, '');

// We only need the first item - return a flat array.
$flat_meta =  array_map(function($item) {
	return $item[0];
}, $meta);

return array_filter($flat_meta, function ($key){
	return substr($key, 0, 1) !== '_';
}, ARRAY_FILTER_USE_KEY);

}
`

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