Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: meta.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Core Metadata API
[2] Fix | Delete
*
[3] Fix | Delete
* Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
[4] Fix | Delete
* for an object is a represented by a simple key-value pair. Objects may contain multiple
[5] Fix | Delete
* metadata entries that share the same key and differ only in their value.
[6] Fix | Delete
*
[7] Fix | Delete
* @package WordPress
[8] Fix | Delete
* @subpackage Meta
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php';
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Adds metadata for the specified object.
[15] Fix | Delete
*
[16] Fix | Delete
* @since 2.9.0
[17] Fix | Delete
*
[18] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[19] Fix | Delete
*
[20] Fix | Delete
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
[21] Fix | Delete
* or any other object type with an associated meta table.
[22] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[23] Fix | Delete
* @param string $meta_key Metadata key.
[24] Fix | Delete
* @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and
[25] Fix | Delete
* will be returned as the same type when retrieved. Other data types will
[26] Fix | Delete
* be stored as strings in the database:
[27] Fix | Delete
* - false is stored and retrieved as an empty string ('')
[28] Fix | Delete
* - true is stored and retrieved as '1'
[29] Fix | Delete
* - numbers (both integer and float) are stored and retrieved as strings
[30] Fix | Delete
* Must be serializable if non-scalar.
[31] Fix | Delete
* @param bool $unique Optional. Whether the specified metadata key should be unique for the object.
[32] Fix | Delete
* If true, and the object already has a value for the specified metadata key,
[33] Fix | Delete
* no change will be made. Default false.
[34] Fix | Delete
* @return int|false The meta ID on success, false on failure.
[35] Fix | Delete
*/
[36] Fix | Delete
function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
[37] Fix | Delete
global $wpdb;
[38] Fix | Delete
[39] Fix | Delete
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
[40] Fix | Delete
return false;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$object_id = absint( $object_id );
[44] Fix | Delete
if ( ! $object_id ) {
[45] Fix | Delete
return false;
[46] Fix | Delete
}
[47] Fix | Delete
[48] Fix | Delete
$table = _get_meta_table( $meta_type );
[49] Fix | Delete
if ( ! $table ) {
[50] Fix | Delete
return false;
[51] Fix | Delete
}
[52] Fix | Delete
[53] Fix | Delete
$meta_subtype = get_object_subtype( $meta_type, $object_id );
[54] Fix | Delete
[55] Fix | Delete
$column = sanitize_key( $meta_type . '_id' );
[56] Fix | Delete
[57] Fix | Delete
// expected_slashed ($meta_key)
[58] Fix | Delete
$meta_key = wp_unslash( $meta_key );
[59] Fix | Delete
$meta_value = wp_unslash( $meta_value );
[60] Fix | Delete
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Short-circuits adding metadata of a specific type.
[64] Fix | Delete
*
[65] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[66] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[67] Fix | Delete
* Returning a non-null value will effectively short-circuit the function.
[68] Fix | Delete
*
[69] Fix | Delete
* Possible hook names include:
[70] Fix | Delete
*
[71] Fix | Delete
* - `add_post_metadata`
[72] Fix | Delete
* - `add_comment_metadata`
[73] Fix | Delete
* - `add_term_metadata`
[74] Fix | Delete
* - `add_user_metadata`
[75] Fix | Delete
*
[76] Fix | Delete
* @since 3.1.0
[77] Fix | Delete
*
[78] Fix | Delete
* @param null|bool $check Whether to allow adding metadata for the given type.
[79] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[80] Fix | Delete
* @param string $meta_key Metadata key.
[81] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[82] Fix | Delete
* @param bool $unique Whether the specified meta key should be unique for the object.
[83] Fix | Delete
*/
[84] Fix | Delete
$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
[85] Fix | Delete
if ( null !== $check ) {
[86] Fix | Delete
return $check;
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
if ( $unique && $wpdb->get_var(
[90] Fix | Delete
$wpdb->prepare(
[91] Fix | Delete
"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
[92] Fix | Delete
$meta_key,
[93] Fix | Delete
$object_id
[94] Fix | Delete
)
[95] Fix | Delete
) ) {
[96] Fix | Delete
return false;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
$_meta_value = $meta_value;
[100] Fix | Delete
$meta_value = maybe_serialize( $meta_value );
[101] Fix | Delete
[102] Fix | Delete
/**
[103] Fix | Delete
* Fires immediately before meta of a specific type is added.
[104] Fix | Delete
*
[105] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[106] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[107] Fix | Delete
*
[108] Fix | Delete
* Possible hook names include:
[109] Fix | Delete
*
[110] Fix | Delete
* - `add_post_meta`
[111] Fix | Delete
* - `add_comment_meta`
[112] Fix | Delete
* - `add_term_meta`
[113] Fix | Delete
* - `add_user_meta`
[114] Fix | Delete
*
[115] Fix | Delete
* @since 3.1.0
[116] Fix | Delete
*
[117] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[118] Fix | Delete
* @param string $meta_key Metadata key.
[119] Fix | Delete
* @param mixed $_meta_value Metadata value.
[120] Fix | Delete
*/
[121] Fix | Delete
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
[122] Fix | Delete
[123] Fix | Delete
$result = $wpdb->insert(
[124] Fix | Delete
$table,
[125] Fix | Delete
array(
[126] Fix | Delete
$column => $object_id,
[127] Fix | Delete
'meta_key' => $meta_key,
[128] Fix | Delete
'meta_value' => $meta_value,
[129] Fix | Delete
)
[130] Fix | Delete
);
[131] Fix | Delete
[132] Fix | Delete
if ( ! $result ) {
[133] Fix | Delete
return false;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
$mid = (int) $wpdb->insert_id;
[137] Fix | Delete
[138] Fix | Delete
wp_cache_delete( $object_id, $meta_type . '_meta' );
[139] Fix | Delete
[140] Fix | Delete
/**
[141] Fix | Delete
* Fires immediately after meta of a specific type is added.
[142] Fix | Delete
*
[143] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[144] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[145] Fix | Delete
*
[146] Fix | Delete
* Possible hook names include:
[147] Fix | Delete
*
[148] Fix | Delete
* - `added_post_meta`
[149] Fix | Delete
* - `added_comment_meta`
[150] Fix | Delete
* - `added_term_meta`
[151] Fix | Delete
* - `added_user_meta`
[152] Fix | Delete
*
[153] Fix | Delete
* @since 2.9.0
[154] Fix | Delete
*
[155] Fix | Delete
* @param int $mid The meta ID after successful update.
[156] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[157] Fix | Delete
* @param string $meta_key Metadata key.
[158] Fix | Delete
* @param mixed $_meta_value Metadata value.
[159] Fix | Delete
*/
[160] Fix | Delete
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
[161] Fix | Delete
[162] Fix | Delete
return $mid;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
/**
[166] Fix | Delete
* Updates metadata for the specified object. If no value already exists for the specified object
[167] Fix | Delete
* ID and metadata key, the metadata will be added.
[168] Fix | Delete
*
[169] Fix | Delete
* @since 2.9.0
[170] Fix | Delete
*
[171] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[172] Fix | Delete
*
[173] Fix | Delete
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
[174] Fix | Delete
* or any other object type with an associated meta table.
[175] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[176] Fix | Delete
* @param string $meta_key Metadata key.
[177] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[178] Fix | Delete
* @param mixed $prev_value Optional. Previous value to check before updating.
[179] Fix | Delete
* If specified, only update existing metadata entries with
[180] Fix | Delete
* this value. Otherwise, update all entries. Default empty string.
[181] Fix | Delete
* @return int|bool The new meta field ID if a field with the given key didn't exist
[182] Fix | Delete
* and was therefore added, true on successful update,
[183] Fix | Delete
* false on failure or if the value passed to the function
[184] Fix | Delete
* is the same as the one that is already in the database.
[185] Fix | Delete
*/
[186] Fix | Delete
function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
[187] Fix | Delete
global $wpdb;
[188] Fix | Delete
[189] Fix | Delete
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
[190] Fix | Delete
return false;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
$object_id = absint( $object_id );
[194] Fix | Delete
if ( ! $object_id ) {
[195] Fix | Delete
return false;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
$table = _get_meta_table( $meta_type );
[199] Fix | Delete
if ( ! $table ) {
[200] Fix | Delete
return false;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
$meta_subtype = get_object_subtype( $meta_type, $object_id );
[204] Fix | Delete
[205] Fix | Delete
$column = sanitize_key( $meta_type . '_id' );
[206] Fix | Delete
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
[207] Fix | Delete
[208] Fix | Delete
// expected_slashed ($meta_key)
[209] Fix | Delete
$raw_meta_key = $meta_key;
[210] Fix | Delete
$meta_key = wp_unslash( $meta_key );
[211] Fix | Delete
$passed_value = $meta_value;
[212] Fix | Delete
$meta_value = wp_unslash( $meta_value );
[213] Fix | Delete
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Short-circuits updating metadata of a specific type.
[217] Fix | Delete
*
[218] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[219] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[220] Fix | Delete
* Returning a non-null value will effectively short-circuit the function.
[221] Fix | Delete
*
[222] Fix | Delete
* Possible hook names include:
[223] Fix | Delete
*
[224] Fix | Delete
* - `update_post_metadata`
[225] Fix | Delete
* - `update_comment_metadata`
[226] Fix | Delete
* - `update_term_metadata`
[227] Fix | Delete
* - `update_user_metadata`
[228] Fix | Delete
*
[229] Fix | Delete
* @since 3.1.0
[230] Fix | Delete
*
[231] Fix | Delete
* @param null|bool $check Whether to allow updating metadata for the given type.
[232] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[233] Fix | Delete
* @param string $meta_key Metadata key.
[234] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[235] Fix | Delete
* @param mixed $prev_value Optional. Previous value to check before updating.
[236] Fix | Delete
* If specified, only update existing metadata entries with
[237] Fix | Delete
* this value. Otherwise, update all entries.
[238] Fix | Delete
*/
[239] Fix | Delete
$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
[240] Fix | Delete
if ( null !== $check ) {
[241] Fix | Delete
return (bool) $check;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
// Compare existing value to new value if no prev value given and the key exists only once.
[245] Fix | Delete
if ( empty( $prev_value ) ) {
[246] Fix | Delete
$old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
[247] Fix | Delete
if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
[248] Fix | Delete
if ( $old_value[0] === $meta_value ) {
[249] Fix | Delete
return false;
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
[255] Fix | Delete
if ( empty( $meta_ids ) ) {
[256] Fix | Delete
return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
$_meta_value = $meta_value;
[260] Fix | Delete
$meta_value = maybe_serialize( $meta_value );
[261] Fix | Delete
[262] Fix | Delete
$data = compact( 'meta_value' );
[263] Fix | Delete
$where = array(
[264] Fix | Delete
$column => $object_id,
[265] Fix | Delete
'meta_key' => $meta_key,
[266] Fix | Delete
);
[267] Fix | Delete
[268] Fix | Delete
if ( ! empty( $prev_value ) ) {
[269] Fix | Delete
$prev_value = maybe_serialize( $prev_value );
[270] Fix | Delete
$where['meta_value'] = $prev_value;
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
foreach ( $meta_ids as $meta_id ) {
[274] Fix | Delete
/**
[275] Fix | Delete
* Fires immediately before updating metadata of a specific type.
[276] Fix | Delete
*
[277] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[278] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[279] Fix | Delete
*
[280] Fix | Delete
* Possible hook names include:
[281] Fix | Delete
*
[282] Fix | Delete
* - `update_post_meta`
[283] Fix | Delete
* - `update_comment_meta`
[284] Fix | Delete
* - `update_term_meta`
[285] Fix | Delete
* - `update_user_meta`
[286] Fix | Delete
*
[287] Fix | Delete
* @since 2.9.0
[288] Fix | Delete
*
[289] Fix | Delete
* @param int $meta_id ID of the metadata entry to update.
[290] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[291] Fix | Delete
* @param string $meta_key Metadata key.
[292] Fix | Delete
* @param mixed $_meta_value Metadata value.
[293] Fix | Delete
*/
[294] Fix | Delete
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
[295] Fix | Delete
[296] Fix | Delete
if ( 'post' === $meta_type ) {
[297] Fix | Delete
/**
[298] Fix | Delete
* Fires immediately before updating a post's metadata.
[299] Fix | Delete
*
[300] Fix | Delete
* @since 2.9.0
[301] Fix | Delete
*
[302] Fix | Delete
* @param int $meta_id ID of metadata entry to update.
[303] Fix | Delete
* @param int $object_id Post ID.
[304] Fix | Delete
* @param string $meta_key Metadata key.
[305] Fix | Delete
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
[306] Fix | Delete
* if the value is an array, an object, or itself a PHP-serialized string.
[307] Fix | Delete
*/
[308] Fix | Delete
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$result = $wpdb->update( $table, $data, $where );
[313] Fix | Delete
if ( ! $result ) {
[314] Fix | Delete
return false;
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
wp_cache_delete( $object_id, $meta_type . '_meta' );
[318] Fix | Delete
[319] Fix | Delete
foreach ( $meta_ids as $meta_id ) {
[320] Fix | Delete
/**
[321] Fix | Delete
* Fires immediately after updating metadata of a specific type.
[322] Fix | Delete
*
[323] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[324] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[325] Fix | Delete
*
[326] Fix | Delete
* Possible hook names include:
[327] Fix | Delete
*
[328] Fix | Delete
* - `updated_post_meta`
[329] Fix | Delete
* - `updated_comment_meta`
[330] Fix | Delete
* - `updated_term_meta`
[331] Fix | Delete
* - `updated_user_meta`
[332] Fix | Delete
*
[333] Fix | Delete
* @since 2.9.0
[334] Fix | Delete
*
[335] Fix | Delete
* @param int $meta_id ID of updated metadata entry.
[336] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[337] Fix | Delete
* @param string $meta_key Metadata key.
[338] Fix | Delete
* @param mixed $_meta_value Metadata value.
[339] Fix | Delete
*/
[340] Fix | Delete
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
[341] Fix | Delete
[342] Fix | Delete
if ( 'post' === $meta_type ) {
[343] Fix | Delete
/**
[344] Fix | Delete
* Fires immediately after updating a post's metadata.
[345] Fix | Delete
*
[346] Fix | Delete
* @since 2.9.0
[347] Fix | Delete
*
[348] Fix | Delete
* @param int $meta_id ID of updated metadata entry.
[349] Fix | Delete
* @param int $object_id Post ID.
[350] Fix | Delete
* @param string $meta_key Metadata key.
[351] Fix | Delete
* @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value
[352] Fix | Delete
* if the value is an array, an object, or itself a PHP-serialized string.
[353] Fix | Delete
*/
[354] Fix | Delete
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
[355] Fix | Delete
}
[356] Fix | Delete
}
[357] Fix | Delete
[358] Fix | Delete
return true;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
/**
[362] Fix | Delete
* Deletes metadata for the specified object.
[363] Fix | Delete
*
[364] Fix | Delete
* @since 2.9.0
[365] Fix | Delete
*
[366] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[367] Fix | Delete
*
[368] Fix | Delete
* @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
[369] Fix | Delete
* or any other object type with an associated meta table.
[370] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[371] Fix | Delete
* @param string $meta_key Metadata key.
[372] Fix | Delete
* @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar.
[373] Fix | Delete
* If specified, only delete metadata entries with this value.
[374] Fix | Delete
* Otherwise, delete all entries with the specified meta_key.
[375] Fix | Delete
* Pass `null`, `false`, or an empty string to skip this check.
[376] Fix | Delete
* (For backward compatibility, it is not possible to pass an empty string
[377] Fix | Delete
* to delete those entries with an empty string for a value.)
[378] Fix | Delete
* Default empty string.
[379] Fix | Delete
* @param bool $delete_all Optional. If true, delete matching metadata entries for all objects,
[380] Fix | Delete
* ignoring the specified object_id. Otherwise, only delete
[381] Fix | Delete
* matching metadata entries for the specified object_id. Default false.
[382] Fix | Delete
* @return bool True on successful delete, false on failure.
[383] Fix | Delete
*/
[384] Fix | Delete
function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
[385] Fix | Delete
global $wpdb;
[386] Fix | Delete
[387] Fix | Delete
if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
[388] Fix | Delete
return false;
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
$object_id = absint( $object_id );
[392] Fix | Delete
if ( ! $object_id && ! $delete_all ) {
[393] Fix | Delete
return false;
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
$table = _get_meta_table( $meta_type );
[397] Fix | Delete
if ( ! $table ) {
[398] Fix | Delete
return false;
[399] Fix | Delete
}
[400] Fix | Delete
[401] Fix | Delete
$type_column = sanitize_key( $meta_type . '_id' );
[402] Fix | Delete
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
[403] Fix | Delete
[404] Fix | Delete
// expected_slashed ($meta_key)
[405] Fix | Delete
$meta_key = wp_unslash( $meta_key );
[406] Fix | Delete
$meta_value = wp_unslash( $meta_value );
[407] Fix | Delete
[408] Fix | Delete
/**
[409] Fix | Delete
* Short-circuits deleting metadata of a specific type.
[410] Fix | Delete
*
[411] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[412] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[413] Fix | Delete
* Returning a non-null value will effectively short-circuit the function.
[414] Fix | Delete
*
[415] Fix | Delete
* Possible hook names include:
[416] Fix | Delete
*
[417] Fix | Delete
* - `delete_post_metadata`
[418] Fix | Delete
* - `delete_comment_metadata`
[419] Fix | Delete
* - `delete_term_metadata`
[420] Fix | Delete
* - `delete_user_metadata`
[421] Fix | Delete
*
[422] Fix | Delete
* @since 3.1.0
[423] Fix | Delete
*
[424] Fix | Delete
* @param null|bool $delete Whether to allow metadata deletion of the given type.
[425] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[426] Fix | Delete
* @param string $meta_key Metadata key.
[427] Fix | Delete
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
[428] Fix | Delete
* @param bool $delete_all Whether to delete the matching metadata entries
[429] Fix | Delete
* for all objects, ignoring the specified $object_id.
[430] Fix | Delete
* Default false.
[431] Fix | Delete
*/
[432] Fix | Delete
$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
[433] Fix | Delete
if ( null !== $check ) {
[434] Fix | Delete
return (bool) $check;
[435] Fix | Delete
}
[436] Fix | Delete
[437] Fix | Delete
$_meta_value = $meta_value;
[438] Fix | Delete
$meta_value = maybe_serialize( $meta_value );
[439] Fix | Delete
[440] Fix | Delete
$query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
[441] Fix | Delete
[442] Fix | Delete
if ( ! $delete_all ) {
[443] Fix | Delete
$query .= $wpdb->prepare( " AND $type_column = %d", $object_id );
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
[447] Fix | Delete
$query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
$meta_ids = $wpdb->get_col( $query );
[451] Fix | Delete
if ( ! count( $meta_ids ) ) {
[452] Fix | Delete
return false;
[453] Fix | Delete
}
[454] Fix | Delete
[455] Fix | Delete
if ( $delete_all ) {
[456] Fix | Delete
if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
[457] Fix | Delete
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );
[458] Fix | Delete
} else {
[459] Fix | Delete
$object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
[460] Fix | Delete
}
[461] Fix | Delete
}
[462] Fix | Delete
[463] Fix | Delete
/**
[464] Fix | Delete
* Fires immediately before deleting metadata of a specific type.
[465] Fix | Delete
*
[466] Fix | Delete
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
[467] Fix | Delete
* (post, comment, term, user, or any other type with an associated meta table).
[468] Fix | Delete
*
[469] Fix | Delete
* Possible hook names include:
[470] Fix | Delete
*
[471] Fix | Delete
* - `delete_post_meta`
[472] Fix | Delete
* - `delete_comment_meta`
[473] Fix | Delete
* - `delete_term_meta`
[474] Fix | Delete
* - `delete_user_meta`
[475] Fix | Delete
*
[476] Fix | Delete
* @since 3.1.0
[477] Fix | Delete
*
[478] Fix | Delete
* @param string[] $meta_ids An array of metadata entry IDs to delete.
[479] Fix | Delete
* @param int $object_id ID of the object metadata is for.
[480] Fix | Delete
* @param string $meta_key Metadata key.
[481] Fix | Delete
* @param mixed $_meta_value Metadata value.
[482] Fix | Delete
*/
[483] Fix | Delete
do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
[484] Fix | Delete
[485] Fix | Delete
// Old-style action.
[486] Fix | Delete
if ( 'post' === $meta_type ) {
[487] Fix | Delete
/**
[488] Fix | Delete
* Fires immediately before deleting metadata for a post.
[489] Fix | Delete
*
[490] Fix | Delete
* @since 2.9.0
[491] Fix | Delete
*
[492] Fix | Delete
* @param string[] $meta_ids An array of metadata entry IDs to delete.
[493] Fix | Delete
*/
[494] Fix | Delete
do_action( 'delete_postmeta', $meta_ids );
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';
[498] Fix | Delete
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function