get_metadata( string $meta_type, int $object_id, string $meta_key = '', bool $single = false )

Retrieve metadata for the specified object.


Description Description


Parameters Parameters

$meta_type

(string) (Required) Type of object metadata is for (e.g., comment, post, term, or user).

$object_id

(int) (Required) ID of the object metadata is for

$meta_key

(string) (Optional) Metadata key. If not specified, retrieve all metadata for the specified object.

Default value: ''

$single

(bool) (Optional) If true, return only the first value of the specified meta_key. This parameter has no effect if meta_key is not specified.

Default value: false


Top ↑

Return Return

(mixed) Single metadata value, or array of values


Top ↑

Source Source

File: wp-includes/meta.php

486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
    if ( ! $meta_type || ! is_numeric( $object_id ) ) {
        return false;
    }
 
    $object_id = absint( $object_id );
    if ( ! $object_id ) {
        return false;
    }
 
    /**
     * Filters whether to retrieve metadata of a specific type.
     *
     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     * object type (comment, post, term, or user). Returning a non-null value
     * will effectively short-circuit the function.
     *
     * @since 3.1.0
     *
     * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
     *                                     or an array of values.
     * @param int               $object_id Object ID.
     * @param string            $meta_key  Meta key.
     * @param bool              $single    Whether to return only the first value of the specified $meta_key.
     */
    $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
    if ( null !== $check ) {
        if ( $single && is_array( $check ) ) {
            return $check[0];
        } else {
            return $check;
        }
    }
 
    $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
 
    if ( ! $meta_cache ) {
        $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
        $meta_cache = $meta_cache[ $object_id ];
    }
 
    if ( ! $meta_key ) {
        return $meta_cache;
    }
 
    if ( isset( $meta_cache[ $meta_key ] ) ) {
        if ( $single ) {
            return maybe_unserialize( $meta_cache[ $meta_key ][0] );
        } else {
            return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
        }
    }
 
    if ( $single ) {
        return '';
    } else {
        return array();
    }
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.