Using WordPress ‘delete_metadata_by_mid()’ PHP function

The delete_metadata_by_mid() WordPress PHP function is used to delete metadata based on the meta ID.

Usage

This function can be used to remove a specific metadata entry associated with any WordPress object type. Here’s a quick example:

delete_metadata_by_mid('post', 123);

In this example, the function will delete the post metadata with the ID of 123.

Parameters

  • $meta_type (string): The type of object metadata is for. This can be ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.
  • $meta_id (int): The ID for a specific meta row.

More information

See WordPress Developer Resources: delete_metadata_by_mid()
This function was introduced in WordPress 3.4.0.

Examples

Deleting Post Metadata

In the following example, we’re deleting a metadata row from the ‘post’ meta table with a meta ID of 1.

delete_metadata_by_mid('post', 1);

This will remove the metadata with ID 1 from the ‘post’ meta table.

Deleting Comment Metadata

Here, we’re deleting a metadata row from the ‘comment’ meta table with a meta ID of 2.

delete_metadata_by_mid('comment', 2);

This removes the metadata with ID 2 from the ‘comment’ meta table.

Deleting Term Metadata

In this instance, we’re deleting a metadata row from the ‘term’ meta table with a meta ID of 3.

delete_metadata_by_mid('term', 3);

This will remove the metadata with ID 3 from the ‘term’ meta table.

Deleting User Metadata

This example demonstrates deleting a metadata row from the ‘user’ meta table with a meta ID of 4.

delete_metadata_by_mid('user', 4);

This will remove the metadata with ID 4 from the ‘user’ meta table.

Deleting Metadata for a Custom Object Type

Lastly, if you have a custom object type with an associated meta table named ‘product’, you can delete a metadata row with a meta ID of 5 like this:

delete_metadata_by_mid('product', 5);

This removes the metadata with ID 5 from the ‘product’ meta table.