get_post_custom_keys()
Retrieve meta field names for a post.
If there are no meta fields, then nothing (null) will be returned.
Uses: get_post_custom()
Used By: the_meta()
No Hooks.
Return
Array|null
. Array of the keys, if retrieved.
Usage
get_post_custom_keys( $post_id );
- $post_id(int)
- Post ID.
Default: ID of the global $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.7.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; } }