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 Description

The dynamic portion of the hook, $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 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 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 Source

File: wp-includes/meta.php

View on Trac



Top ↑

Changelog Changelog

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

Top ↑

User Contributed Notes 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 stevenlinx

    Example migrated from Codex:

    add_action( 'init', 'myplugin_init' );
    
    function myplugin_init() {
    	add_filter( 'get_user_metadata', 'myplugin_get_foo', 10, 4 );
    }
    
    function myplugin_get_foo( $null, $object_id, $meta_key, $single ) {
    	if ( 'foo' == $meta_key ) {
    		// Always return an array with your return value. There is no need to check $single. This logic is handled in wp-includes/meta.php
    		return array( 'bar' );
    	}
    
    	return null; // Go on with the normal execution in meta.php
    }
    

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