apply_filters( “get_{$meta_type}_metadata”, mixed $value, int $object_id, string $meta_key, bool $single, string $meta_type )

Short-circuits the return value of a meta field.

Description

The dynamic portion of the hook name, $meta_type, refers to the meta object type (post, comment, term, user, or any other type with an associated meta table).
Returning a non-null value will effectively short-circuit the function.

Possible filter names include:

  • get_post_metadata
  • get_comment_metadata
  • get_term_metadata
  • get_user_metadata

Parameters

$valuemixed
The value to return, either a single metadata value or an array of values depending on the value of $single. Default null.
$object_idint
ID of the object metadata is for.
$meta_keystring
Metadata key.
$singlebool
Whether to return only the first value of the specified $meta_key.
$meta_typestring
Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.

More Information

The filter must return null if the data should be taken from the database. If it returns anything else, the get_metadata() function (and therefore the get_user_meta) will return what the filter returns.

Source

$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );

Changelog

VersionDescription
5.5.0Added the $meta_type parameter.
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Note that although this filter filters the value that will be returned, you do not receive the current value as the first parameter.

    You allways receive null in the first parameter.

    If your callback returns anything else than null, this is the value that the function will return at the end. If you return null, the function will continue to run normally and try to fetch the data from the database (or cache).

  2. Skip to note 4 content

    Example migrated from Codex:

    add_action( 'init', 'wpdocs_init' );
    
    function wpdocs_init() {
    	add_filter( 'get_user_metadata', 'wpdocs_get_foo', 10, 3 );
    }
    
    function wpdocs_get_foo( $check, $object_id, $meta_key ) {
    	if ( 'foo' === $meta_key ) {
    		// Always return an array with your return value.
    		return array( 'bar' );
    	}
    
    	return $check; // Go on with the normal execution in meta.php
    }

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