Codex

Function Reference/get post custom

Contents

Description

Returns a multidimensional array with all custom fields of a particular post or page. See also get_post_custom_keys() and get_post_custom_values()

Usage

 <?php get_post_custom($post_id); ?> 

Examples

Default Usage

Use the following example to set a variable ($custom_fields) as a multidimensional array containing all custom fields of the current post.

<?php $custom_fields = get_post_custom(); ?>

Retrieving data from the array

The following example will retrieve all custom field values with the key my_custom_field from post ID 72 (assuming there are three custom fields with this key, and the values are "dogs", "47" and "This is another value").

<?php

  $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 />";

?>

0 => dogs
1 => 47
2 => This is another value

Parameters

$post_id
(integer) (optional) The post ID whose custom fields will be retrieved.
Default: Current post

Related

add_post_meta(), delete_post_meta(), get_post_meta(), update_post_meta(), get_post_custom_values(), get_post_custom_keys()