wppaste
WordPress

delete_metadata_by_mid( string $meta_type, int $meta_id ): bool

Since
3.3.0
Source
wp-includes/meta.php:996
Deletes metadata by meta ID.

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

Only touches the object cache, via wp_cache_delete().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–113

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 120.

Plugin surface
5 hooks

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.

Called by
11

11 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_delete()called directly
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
always5–7none
$meta_id12–16floor()
$meta_id == false21floor(), _get_meta_table()
$meta_id == false && $check !== null42–43floor(), _get_meta_table(), sanitize_key(), apply_filters()
$meta_id == false && $check === null47–48floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid()
$meta_id == false && $check === null && $meta_type !== "post" && $meta_type !== "comment"98–99floor(), _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–106floor(), _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–106floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), do_action(), wp_cache_delete(), do_action(), do_action()
$meta_id == false && $check === null108–113floor(), _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

PHPCompiledExecutedBranchesNotes
8.6-dev1205–11312
8.51205–11312
8.41205–113122 fewer instructions than PHP 8.3
8.31225–11512
8.21225–11512
8.11225–11512
7.41225–11512

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:

  1. apply_filters( delete_{$meta_type}_metadata_by_mid )filterline 1037 (+41 into the body)

    Short-circuits deleting metadata of a specific type by meta ID.

  2. do_action( delete_{$meta_type}_meta )actionline 1048 (+52 into the body)

    Fires immediately before deleting metadata of a specific type.

  3. do_action( delete_{$meta_type}meta )actionline 1069 (+73 into the body)

    Fires immediately before deleting post or comment metadata of a specific type.

  4. do_action( deleted_{$meta_type}_meta )actionline 1079 (+83 into the body)

    Fires immediately after deleting metadata of a specific type.

  5. do_action( deleted_{$meta_type}meta )actionline 1100 (+104 into the body)

    Fires immediately after deleting post or comment metadata of a specific type.

Uses · 6

Used by · 11

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.