Retrieves meta field names for a post.
Description
If there are no meta fields, then nothing (null) will be returned.
Parameters
$post_id
intoptional- Post ID. Default is the ID of the global
$post
.
Source
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;
}
}
Changelog
Version | Description |
---|---|
1.2.0 | Introduced. |
Default Usage
The following example will set a variable (
$custom_field_keys
) as an array containing the keys of all custom fields in the current post, and then print it. Note: the if test excludes values for WordPress internally maintained custom keys such as_edit_last
and_edit_lock
.If the post contains custom fields with the keys
mykey
andyourkey
, the output would be something like:Note: Regardless of how many values (custom fields) are assigned to one key, that key will only appear once in this array.