get_post_custom_keys()
Gets an array of keys of custom fields belonging to the current post.
Uses: get_post_custom()
Used By: the_meta()
No Hooks.
Returns
Array|null. Array of meta-field keys.
Usage
get_post_custom_keys($post_id);
- $post_id(integer)
- ID of the post whose custom field keys need to be retrieved.
Default: Current post (determined by the variable $post)
Examples
#1 Get the meta-field keys of the current post
The following example will pass the variable $custom_field_keys - an array containing the keys of the custom fields of the current post and then display those keys.
$custom_field_keys = get_post_custom_keys();
foreach ( $custom_field_keys as $key => $value ) {
$val = trim( $value );
if ( '_' === $val[0] ) {
continue;
}
echo "$key => $value<br />";
}
/* Result:
Suppose the post contains custom fields named 'mykey' and 'yourkey',
then we will see on the screen:
0 => mykey
1 => yourkey
*/
If you remove the condition if ( '_' === $val[0] ) from the loop, the internal custom post fields that WordPress uses, such as _edit_last and _edit_lock, will also be displayed. This condition is needed to exclude such custom field keys.
Changelog
| Since 1.2.0 | Introduced. |
get_post_custom_keys() get post custom keys code WP 6.9.1
function get_post_custom_keys( $post_id = 0 ) {
$custom = get_post_custom( $post_id );
if ( ! is_array( $custom ) ) {
return;
}
$keys = array_keys( $custom );
if ( $keys ) {
return $keys;
}
}