get_post_custom_values()
Retrieve values for a custom post field.
The parameters must not be considered optional. All of the post meta fields will be retrieved and only the meta field key values returned.
Uses: get_post_custom()
No Hooks.
Return
Array|null
. Meta field values.
Usage
get_post_custom_values( $key, $post_id );
- $key(string)
- Meta field key.
Default: '' - $post_id(int)
- Post ID.
Default: ID of the global $post
Examples
#1 Display the values of the custom fields on the screen
Suppose the current post has 3 custom fields with the same key my_key
, let's display them:
$mykey_values = get_post_custom_values('my_key'); foreach( $mykey_values as $key => $value ) { echo "$key => $value ('my_key')<br />"; }
As a result, we will see on the screen:
0 => first value of 'my_key'
1 => second value of 'my_key'
2 => third value of 'my_key'
Changelog
Since 1.2.0 | Introduced. |
get_post_custom_values() get post custom values code WP 6.6.2
function get_post_custom_values( $key = '', $post_id = 0 ) { if ( ! $key ) { return null; } $custom = get_post_custom( $post_id ); return isset( $custom[ $key ] ) ? $custom[ $key ] : null; }