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
$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.
Source
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );
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).
Example migrated from Codex:
add_filter( 'get_user_metadata', 'otherplugin_get_foo', 10, 4 ); function otherplugin_get_foo( $value, $object_id, $meta_key, $single ) { if ( 'foo' == $meta_key ) { return array( 'bar' ); } return $value; // CORRECT if $value is null it will return null, if another plugin has already used the same filter to set a value it will return that value; } add_filter( 'get_user_metadata', 'myplugin_get_baz', 10, 4 ); function otherplugin_get_foo( $value, $object_id, $meta_key, $single ) { if ( 'baz' == $meta_key ) { return array( 'bar' ); } return null; // WRONG! THIS WILL OVERRIDE ALL PREVIOUS get_{$meta_type}_metadata FILTERS; }
Now if you call get_user_meta(1, ‘foo’, true); you will always get null, because myplugin_get_baz will override the $value (bar) set by the first otherplugin_get_foo filter. Always return the first parameter if you’re not going to edit the value.