The get_{$meta_type}_metadata WordPress PHP filter short-circuits the return value of a meta field. The dynamic portion, $meta_type, refers to the meta object type (post, comment, term, user, or any other type with an associated meta table).
Usage
add_filter( 'get_post_metadata', 'your_custom_function', 10, 4 ); function your_custom_function( $value, $object_id, $meta_key, $single ) { // your custom code here return $value; }
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.
More information
See WordPress Developer Resources: get_{$meta_type}_metadata
Examples
Modify post metadata
Modify the value of a specific post metadata.
add_filter( 'get_post_metadata', 'modify_post_metadata', 10, 4 ); function modify_post_metadata( $value, $object_id, $meta_key, $single ) { if ( 'custom_key' === $meta_key ) { $value = 'New Value'; } return $value; }
Hide user metadata
Hide a specific user metadata.
add_filter( 'get_user_metadata', 'hide_user_metadata', 10, 4 ); function hide_user_metadata( $value, $object_id, $meta_key, $single ) { if ( 'hidden_key' === $meta_key ) { return false; } return $value; }
Filter term metadata
Filter a specific term metadata.
add_filter( 'get_term_metadata', 'filter_term_metadata', 10, 4 ); function filter_term_metadata( $value, $object_id, $meta_key, $single ) { if ( 'term_key' === $meta_key ) { $value = 'Filtered Value'; } return $value; }
Modify comment metadata
Modify the value of a specific comment metadata.
add_filter( 'get_comment_metadata', 'modify_comment_metadata', 10, 4 ); function modify_comment_metadata( $value, $object_id, $meta_key, $single ) { if ( 'comment_key' === $meta_key ) { $value = 'New Comment Value'; } return $value; }
Short-circuit metadata retrieval
Short-circuit metadata retrieval for a specific key.
add_filter( 'get_post_metadata', 'short_circuit_metadata', 10, 4 ); function short_circuit_metadata( $value, $object_id, $meta_key, $single ) { if ( 'short_circuit_key' === $meta_key ) { return 'Short-circuit Value'; } return $value; }