get_post_custom_keys( int $post_id )

Retrieve meta field names for a post.


Description Description

If there are no meta fields, then nothing (null) will be returned.


Parameters Parameters

$post_id

(int) (Optional) Post ID. Default is ID of the global $post.


Top ↑

Return Return

(array|void) Array of the keys, if retrieved.


Top ↑

Source Source

File: wp-includes/post.php

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;
	}
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.2.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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.

    <?php
    
    $custom_field_keys = get_post_custom_keys();
    foreach ( $custom_field_keys as $key => $value ) {
        $valuet = trim($value);
        if ( '_' == $valuet{0} )
            continue;
        echo $key . " => " . $value . "<br />";
    }
    ?>
    

    If the post contains custom fields with the keys mykey and yourkey, the output would be something like:

    0 => mykey
    1 => yourkey
    

    Note: Regardless of how many values (custom fields) are assigned to one key, that key will only appear once in this array.

You must log in before being able to contribute a note or feedback.