10

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?

1 Answer 1

17

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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