get_post_custom()
Gets a multidimensional array with data from all custom fields of the current post.
Instead of this function, you can use get_post_meta( $post_id ). The difference between them is that get_post_meta() requires the post ID to be passed, while this function will automatically get the ID of the current post if $post_id is not provided.
No Hooks.
Returns
Mixed. Post metadata.
Usage
get_post_custom( $post_id );
- $post_id(integer)
- ID of the post whose custom field data needs to be retrieved.
Default: Current post (determined by the variable $post)
Examples
#1 Get the same post meta-fields with different values
Get the values of custom fields with the key my_custom_field for post 72 (assuming that the post has 3 such fields with the values "dogs", "47" and "selfies")
$custom_fields = get_post_custom( 72 );
$my_custom_field = $custom_fields['my_custom_field'];
foreach ( $my_custom_field as $key => $value ){
echo $key . " => " . $value . "<br />";
}
As a result, we will see the following on the screen:
0 => dogs 1 => 47 2 => selfies
#2 More examples
This function is a wrapper for get_post_meta(), so more examples see there.
#3 Get all post custom fields
Use the following example to pass to the variable $custom_fields all data about the custom fields of the current post.
$custom_fields = get_post_custom();
Changelog
| Since 1.2.0 | Introduced. |
get_post_custom() get post custom code WP 6.9
function get_post_custom( $post_id = 0 ) {
$post_id = absint( $post_id );
if ( ! $post_id ) {
$post_id = get_the_ID();
}
return get_post_meta( $post_id );
}