WordPress函数:delete_metadata 删除指定对象的附加属性值

江河/ 2023年10月02日/ WordPress/ 浏览 641

函数原型:


delete_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value = '', bool $delete_all = false ): bool


删除指定对象的附加属性值。


参数说明:


$meta_type,对象的类型为。接受'post'、'comment'、'term'、'user'或任何其他具有关联元表的对象类型。


$object_id,对象 ID。


$meta_key,属性key。


$meta_value,元数据值。如果不是标量,则必须是可序列化的。如果指定,则仅删除具有此值的元数据条目。否则,请删除具有指定meta_key的所有条目。传递null、false或空字符串以跳过此检查。(为了向后兼容,不可能传递一个空字符串来删除那些值为空字符串的条目。)默认值:“”


$delete_all,如果为true,则删除所有对象的匹配元数据条目,忽略指定的object_id。否则,只删除指定object_id的匹配元数据条目。默认值:false


函数源码:


function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
	global $wpdb;

	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id && ! $delete_all ) {
		return false;
	}

	$table = _get_meta_table( $meta_type );
	if ( ! $table ) {
		return false;
	}

	$type_column = sanitize_key( $meta_type . '_id' );
	$id_column   = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';

	// expected_slashed ($meta_key)
	$meta_key   = wp_unslash( $meta_key );
	$meta_value = wp_unslash( $meta_value );

	$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
	if ( null !== $check ) {
		return (bool) $check;
	}

	$_meta_value = $meta_value;
	$meta_value  = maybe_serialize( $meta_value );

	$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );

	if ( ! $delete_all ) {
		$query .= $wpdb->prepare( " AND $type_column = %d", $object_id );
	}

	if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
		$query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );
	}

	$meta_ids = $wpdb->get_col( $query );
	if ( ! count( $meta_ids ) ) {
		return false;
	}

	if ( $delete_all ) {
		if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
			$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );
		} else {
			$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
		}
	}

	do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );

	// Old-style action.
	if ( 'post' === $meta_type ) {
		do_action( 'delete_postmeta', $meta_ids );
	}

	$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';

	$count = $wpdb->query( $query );

	if ( ! $count ) {
		return false;
	}

	if ( $delete_all ) {
		$data = (array) $object_ids;
	} else {
		$data = array( $object_id );
	}
	wp_cache_delete_multiple( $data, $meta_type . '_meta' );

	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );

	// Old-style action.
	if ( 'post' === $meta_type ) {
		do_action( 'deleted_postmeta', $meta_ids );
	}

	return true;
}


包含钩子:


do_action( 'deleted_postmeta', string[] $meta_ids )

do_action( "deleted_{$meta_type}_meta", string[] $meta_ids, int $object_id, string $meta_key, mixed $_meta_value )

do_action( 'delete_postmeta', string[] $meta_ids )

do_action( "delete_{$meta_type}_meta", string[] $meta_ids, int $object_id, string $meta_key, mixed $_meta_value )

apply_filters( "delete_{$meta_type}_metadata", null|bool $delete, int $object_id, string $meta_key, mixed $meta_value, bool $delete_all )


使用举例:


$meta_type  = 'post';           // since we are deleting data for CPT 
$object_id  = 0;                // no need to put id of object since we are deleting all
$meta_key   = 'my_meta_key';    // Your target meta_key added using update_post_meta()
$meta_value = '';               // No need to check for value since we are deleting all
$delete_all = true;             // This is important to have TRUE to delete all post meta

// This will delete all post meta data having the specified key 
delete_metadata( $meta_type, $object_id, $meta_key, $meta_value, $delete_all );


发表评论

暂无评论,抢个沙发...

客服 工单