On this page
get_metadata_by_mid( string $meta_type, int $meta_id ): stdClass|false
Retrieves metadata by meta ID.
Parameters
$meta_typestring Required-
Type of object metadata is for. Accepts
'post','comment','term','user', or any other object type with an associated meta table. $meta_idint Required-
ID for a specific meta row.
Return
stdClass|false Metadata object, or boolean false if the metadata doesn't exist.
meta_keystringThe meta key.meta_valuemixedThe unserialized meta value.meta_idstringOptional. The meta ID when the meta type is any value except'user'.umeta_idstringOptional. The meta ID when the meta type is'user'.post_idstringOptional. The object ID when the meta type is'post'.comment_idstringOptional. The object ID when the meta type is'comment'.term_idstringOptional. The object ID when the meta type is'term'.user_idstringOptional. The object ID when the meta type is'user'.
Source
File: wp-includes/meta.php. View all references
function get_metadata_by_mid( $meta_type, $meta_id ) {
global $wpdb;
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
return false;
}
$meta_id = (int) $meta_id;
if ( $meta_id <= 0 ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
/**
* Short-circuits the return value when fetching a meta field by meta ID.
*
* 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 hook names include:
*
* - `get_post_metadata_by_mid`
* - `get_comment_metadata_by_mid`
* - `get_term_metadata_by_mid`
* - `get_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param stdClass|null $value The value to return.
* @param int $meta_id Meta ID.
*/
$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
if ( null !== $check ) {
return $check;
}
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
if ( empty( $meta ) ) {
return false;
}
if ( isset( $meta->meta_value ) ) {
$meta->meta_value = maybe_unserialize( $meta->meta_value );
}
return $meta;
}
Hooks
- apply_filters( "get_{$meta_type}_metadata_by_mid",
stdClass|null $value ,int $meta_id ) -
Short-circuits the return value when fetching a meta field by meta ID.
Related
Uses
| Uses | Description |
|---|---|
| maybe_unserialize() wp-includes/functions.php | Unserializes data only if it was serialized. |
| wpdb::get_row() wp-includes/class-wpdb.php | Retrieves one row from the database. |
| _get_meta_table() wp-includes/meta.php | Retrieves the name of the metadata table for the specified object type. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| wp_xmlrpc_server::set_term_custom_fields() wp-includes/class-wp-xmlrpc-server.php | Set custom fields for a term. |
| get_post_meta_by_id() wp-admin/includes/post.php | Returns post meta data by meta ID. |
| wp_ajax_add_meta() wp-admin/includes/ajax-actions.php | Ajax handler for adding meta. |
| wp_ajax_delete_meta() wp-admin/includes/ajax-actions.php | Ajax handler for deleting meta. |
| wp_xmlrpc_server::set_custom_fields() wp-includes/class-wp-xmlrpc-server.php | Set custom fields for post. |
| update_metadata_by_mid() wp-includes/meta.php | Updates metadata by meta ID. |
| delete_metadata_by_mid() wp-includes/meta.php | Deletes metadata by meta ID. |
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_metadata_by_mid