get_post_custom_values()
Gets an array of values of custom fields with the specified name for the specified post.
This function may be useful if you need to get the value of a custom field when the post has multiple fields with the same key names and different values. Otherwise, it is more convenient to use the function get_post_meta().
Uses: get_post_custom()
No Hooks.
Returns
Array|null. An array of meta-field values.
Usage
get_post_custom_values( $key, $post_id );
- $key(string) (required)
- The name of the custom field (custom field key) whose values need to be retrieved.
- $post_id(int)
- The ID of the post whose custom fields need to be retrieved.
Default: Current post (determined by the variable $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.9
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;
}