Skip to main content
added 2 characters in body
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

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

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.

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.

edited body
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

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

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

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

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

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

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

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

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

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

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

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:

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.

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

added an example
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

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

The first call to get_post_meta() will fetch all meta values for that post ID, 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 – to allow multiple values for one key.

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.

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

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

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

The first call to get_post_meta() will fetch all meta values for that post ID, 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 – to allow multiple values for one key.

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.

Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459
Loading