delete_metadata_by_mid( string $meta_type, int $meta_id ): bool
- Since
- 3.3.0
- Source
wp-includes/meta.php:985
Compatibility
- WordPress
- since 3.3.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$meta_typestring- Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$meta_idint- ID for a specific meta row.
Return value
bool- True on successful delete, false on failure.
Performance profile
How much work a call to delete_metadata_by_mid() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Constant
- Instructions
- 5–113
- Plugin surface
- 5 hooks
- Called by
- 11
Only touches the object cache, via wp_cache_delete().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.
Third-party callbacks on 'delete_{$meta_type}_metadata_by_mid', 'delete_{$meta_type}_meta', 'delete_{$meta_type}meta' run inside this call, and their cost is not bounded by anything here.
11 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_delete()called directly - serializeserialisation
maybe_unserialize()one call below delete_metadata_by_mid()
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost delete_metadata_by_mid() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–7 | none |
$meta_id | 12–16 | floor() |
$meta_id == false | 21 | floor(), _get_meta_table() |
$meta_id == false && $check !== null | 42–43 | floor(), _get_meta_table(), sanitize_key(), apply_filters() |
$meta_id == false && $check === null | 47–48 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid() |
$meta_id == false && $check === null && $meta_type !== "post" && $meta_type !== "comment" | 98–99 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), do_action(), wp_cache_delete(), do_action() |
$meta_id == false && $check === null && $meta_type !== "post" && $meta_type !== "comment" | 103–106 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), do_action(), do_action(), wp_cache_delete(), do_action() |
$meta_id == false && $check === null && $meta_type !== "post" && $meta_type !== "comment" | 103–106 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), do_action(), wp_cache_delete(), do_action(), do_action() |
$meta_id == false && $check === null | 108–113 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), do_action(), do_action(), wp_cache_delete(), do_action(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 120 | 5–113 | 12 | |
| 8.5 | 120 | 5–113 | 12 | |
| 8.4 | 120 | 5–113 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 122 | 5–115 | 12 | |
| 8.2 | 122 | 5–115 | 12 | |
| 8.1 | 122 | 5–115 | 12 | |
| 7.4 | 122 | 5–115 | 12 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 5
5 hooks fire while delete_metadata_by_mid() runs, in this order:
- apply_filters( delete_{$meta_type}_metadata_by_mid )filterline 1026 (+41 into the body)
Short-circuits deleting metadata of a specific type by meta ID.
- do_action( delete_{$meta_type}_meta )actionline 1037 (+52 into the body)
Fires immediately before deleting metadata of a specific type.
- do_action( delete_{$meta_type}meta )actionline 1058 (+73 into the body)
Fires immediately before deleting post or comment metadata of a specific type.
- do_action( deleted_{$meta_type}_meta )actionline 1068 (+83 into the body)
Fires immediately after deleting metadata of a specific type.
- do_action( deleted_{$meta_type}meta )actionline 1089 (+104 into the body)
Fires immediately after deleting post or comment metadata of a specific type.
Uses · 6
- _get_meta_table()Retrieves the name of the metadata table for the specified object type.
- sanitize_key()Sanitizes a string key.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_metadata_by_mid()Retrieves metadata by meta ID.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_cache_delete()Removes the cache contents matching key and group.
Used by · 11
- delete_meta()Deletes post meta data by meta ID.
- do_enclose()Checks content for video and audio links to add as enclosures.
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_delete_comment()Trashes or deletes a comment.
- wp_delete_post()Trashes or deletes a post or page.
- wp_delete_site()Deletes a site from the database.
- wp_delete_term()Removes a term from the database.
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- wp_xmlrpc_server::set_custom_fields()Sets custom fields for post.
- wp_xmlrpc_server::set_term_custom_fields()Sets custom fields for a term.
- wpmu_delete_user()Deletes a user and all of their posts from the network.
Source code
function delete_metadata_by_mid( $meta_type, $meta_id ) { global $wpdb; // Make sure everything is valid. 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; } // Object and ID columns. $column = sanitize_key( $meta_type . '_id' ); $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; /** * Short-circuits deleting metadata of a specific type 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: * * - `delete_post_metadata_by_mid` * - `delete_comment_metadata_by_mid` * - `delete_term_metadata_by_mid` * - `delete_user_metadata_by_mid` * * @since 5.0.0 * * @param null|bool $delete Whether to allow metadata deletion of the given type. * @param int $meta_id Meta ID. */ $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id ); if ( null !== $check ) { return (bool) $check; } // Fetch the meta and go on if it's found. $meta = get_metadata_by_mid( $meta_type, $meta_id ); if ( $meta ) { $object_id = (int) $meta->{$column}; /** This action is documented in wp-includes/meta.php */ do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); // Old-style action. if ( 'post' === $meta_type || 'comment' === $meta_type ) { /** * Fires immediately before deleting post or comment metadata of a specific type. * * The dynamic portion of the hook name, `$meta_type`, refers to the meta * object type (post or comment). * * Possible hook names include: * * - `delete_postmeta` * - `delete_commentmeta` * - `delete_termmeta` * - `delete_usermeta` * * @since 3.4.0 * * @param int $meta_id ID of the metadata entry to delete. */ do_action( "delete_{$meta_type}meta", $meta_id ); } // Run the query, will return true if deleted, false otherwise. $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); // Clear the caches.Changelog
Introduced in 3.3.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/meta.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.