Get Meta Values (Deprecated)

UPDATE 2012-07-10: If you’re using Meta Box plugin version >= 4.1.9, please use this documentation to get meta values.

Get value of field with single value

Most fields have single value (this is the default option). To get the value of field, use this code:

$meta = get_post_meta( get_the_ID(), 'meta_key', true );
echo $meta; // If you want to show

The last parameter true indicates that our field has a single value.

The meta_key is the ID of meta field that has been registered. For example, if you registered a field with:

$prefix = 'rw_';
...
array(
    'name' => 'Full name',
    'id' => $prefix . 'fname',
    'type' => 'text',
)

Then meta_key is rw_fname. Note that it is prefixed.

Please read the Codex for more information about get_post_meta().

Get value of field with multiple values

There’re some fields that may have multiple values such as select, checkbox_list. To get (or check) values of these fields, use this code:

$metas = get_post_meta( get_the_ID(), 'meta_key', false );

// If you want to show values
foreach ( $metas as $meta )
{
    echo $meta;
}

// If you want to check if a value $val is set or not
if ( in_array( $val, $metas ) )
{
    // $val is set
    // Do something
}
else
{
    // $val is NOT set
    // Do something
}

The code is very similar to the one above. The different is the last parameter, which now is false, that means our field doesn’t have single value (ie it has multiple values).

Get uploaded files

Uploaded files’ IDs are saved as a field with multiple values. Each uploaded image is treated as an attachment. You can use some built-in functions to get attachments’ url or something else.

The following code will show uploaded files:

$files = get_post_meta( get_the_ID(), 'meta_key', false );
foreach ( $files as $att )
{
    echo wp_get_attachment_link( $att );
}

Get uploaded images

Uploaded images’ IDs are saved as a field with multiple values. Each uploaded image is treated as an attachment. This means it has all sizes that defined in Media section or in your theme functions.php file. So, you can use some built-in functions to get image URL, image size, etc.

The following code will show how to get thumbnail url of uploaded images:

global $wpdb;
$images = get_post_meta( get_the_ID(), 'meta_key', false );
$images = implode( ',' , $images );

// Re-arrange images with 'menu_order'
$images = $wpdb->get_col( "
    SELECT ID FROM {$wpdb->posts}
    WHERE post_type = 'attachment'
    AND ID in ({$images})
    ORDER BY menu_order ASC
" );
foreach ( $images as $att )
{
    // Get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
    $src = wp_get_attachment_image_src( $att, 'full' );
    $src = $src[0];

    // Show image
    echo "<img src='{$src}' />";
}

Updated 1: The images now are saved in the correct order as you made. So, the code to get their IDs is simplified.

Updated 2: People still reported about wrong order of images, so I added the code to reorder them before getting image information.

NEW: Get cloned values

The values of a cloned field are stored as a single entry of a sanitized array. Getting the value of such field is as simple as of field with single value.

$metas = get_post_meta( get_the_ID(), 'meta_key', true );
foreach ( $metas as $meta )
{
    echo $meta;
}

See more tutorials