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

Top ↑

Parameters

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

Top ↑

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.


Top ↑

Source

File: wp-includes/meta.php. View all references

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


Top ↑

Changelog

Changelog
Version Description
5.5.0 Added the $meta_type parameter.
3.1.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by leogermani

    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 2 content
    Contributed by Steven Lin

    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.