get_post_custom()
Retrieve post meta fields, based on post ID.
The post meta fields are retrieved from the cache where possible, so the function is optimized to be called more than once.
No Hooks.
Return
Mixed
. An array of values. False for an invalid $post_id (non-numeric, zero, or negative value). An empty string if a valid but non-existing post ID is passed.
Usage
get_post_custom( $post_id );
- $post_id(int)
- Post ID.
Default: ID of the global $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.7.1
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 ); }