Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to improve my PHP knowledge through test and try! I am confused on [0] arguments at following WordPress PHP code

global $post;
$custom = get_post_custom($post->ID);
$price = $custom['price'][0];
$coMake = $custom['coMake'][0];

echo $custom['price'][0];

Are they for indexing/referencing inputs on the page? if so why they are same?! can you please let me know why and when to use them?

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

Each post meta key can have multiple values. So you get the values by default as an array; the first entry (0) is the oldest.

get_post_custom() fetches all meta keys with their values. You could simplify your code with:

$price  = get_post_meta( $post->ID, 'price', TRUE );
$comake = get_post_meta( $post->ID, 'coMake', TRUE );

When the last parameter for get_post_meta() is TRUE, WordPress will return a single value: the first entry in the post meta value for the given key.

The first call to get_post_meta() will fetch and cache all meta values for that post ID in the background, so later calls will not result in additional database queries.

An example

Let’s say, we create a post meta key named color and fill it with three different values for one post. Then we do the same with a key age.

add_action( 'wp_loaded', function() {

    foreach ( array ( 'blue', 'red', 'yellow' ) as $color )
        add_post_meta( 561, 'color', $color, FALSE );

    foreach ( array ( '14', '40', '104' ) as $age )
        add_post_meta( 561, 'age', $age, FALSE );
});

The last parameter FALSE tells WordPress not to make this meta key unique – but to allow multiple values for one key instead.

The resulting structure in our postmeta table will look like this:

table

Each row has a unique meta_id, but each post_id can have multiple meta_keys, and each meta_key can have multiple meta_values.

Now let’s fetch all custom values for post 561:

add_action( 'shutdown', function(){

    $custom = get_post_custom( 561 );
    print '<pre>' . esc_html( var_export( $custom, TRUE ) ) . '</pre>';
});

Result:

array (
  '_edit_last' => 
  array (
    0 => '1',
  ),
  '_edit_lock' => 
  array (
    0 => '1367617428:1',
  ),
  'color' => 
  array (
    0 => 'blue',
    1 => 'red',
    2 => 'yellow',
  ),
  'age' => 
  array (
    0 => '14',
    1 => '40',
    2 => '104',
  ),
)

_edit_last and _edit_lock are WordPress internals, we ignore that for the moment. Just note, we get these values an an array too, although there is just one value.
color and age are arrays like all post meta values.
$custom['color'][0] is blue.

Now we fetch just the color with get_post_meta():

add_action( 'shutdown', function(){

    $color = get_post_meta( 561, 'color' );
    print '<pre>' . esc_html( var_export( $color, TRUE ) ) . '</pre>';
});

Result:

array (
  0 => 'blue',
  1 => 'red',
  2 => 'yellow',
)

We get just one piece of our meta values here. $color[0] is still blue. Behind the scenes, WordPress has already fetched all custom values, so a next hit for age would be served from the cache.
In terms of performance get_post_custom() and get_post_meta() are equal.

Last attempt: Get the color with get_post_meta() as a single value.

add_action( 'shutdown', function(){

    $color = get_post_meta( 561, 'color', TRUE );
    print '<pre>' . esc_html( var_export( $color, TRUE ) ) . '</pre>';
});

Result: blue.

WordPress has done what you have done in your code: It has taken the array key 0 and returned the value as a string.

This is the most readable option, use it to write code that is easy to understand.

share|improve this answer
    
Hi toscho, thanks for perfect explain but honestly I didn't still get the answer! After your explain I find calling the get_post_meta() more faster than get_post_custom() since we are not retrieving all associated metadata with CPT. but in case of using the get_post_custom(), if the [0] is the indexing value inside the array of metadata values ,as you said, so why they both( 'price', 'coMake') have save index number [0]? And they are working in my code (Customizing Custom Column) without any complain and error? Once again Thanks –  Behseini May 25 '13 at 16:24
    
@Behseini See my update. –  toscho May 25 '13 at 18:37
    
Thanks toscho, this is great help. –  Behseini May 27 '13 at 16:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.