/**
* Metadata API
*
- * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
- * for an object is a represented by a simple key-value pair. Objects may contain multiple
+ * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
+ * for an object is a represented by a simple key-value pair. Objects may contain multiple
* metadata entries that share the same key and differ only in their value.
*
* @package WordPress
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param string $meta_value Metadata value
- * @param bool $unique Optional, default is false. Whether the specified metadata key should be
- * unique for the object. If true, and the object already has a value for the specified
+ * @param bool $unique Optional, default is false. Whether the specified metadata key should be
+ * unique for the object. If true, and the object already has a value for the specified
* metadata key, no change will be made
* @return bool The meta ID on successful update, false on failure.
*/
}
/**
- * Update metadata for the specified object. If no value already exists for the specified object
+ * Update metadata for the specified object. If no value already exists for the specified object
* ID and metadata key, the metadata will be added.
*
* @since 2.9.0
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
* @param string $meta_value Metadata value
- * @param string $prev_value Optional. If specified, only update existing metadata entries with
- * the specified value. Otherwise, update all entries.
+ * @param string $prev_value Optional. If specified, only update existing metadata entries with
+ * the specified value. Otherwise, update all entries.
* @return bool True on successful update, false on failure.
*/
function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
if ( null !== $check )
return (bool) $check;
- if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
- return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
-
// Compare existing value to new value if no prev value given and the key exists only once.
if ( empty($prev_value) ) {
$old_value = get_metadata($meta_type, $object_id, $meta_key);
}
}
+ if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
+ return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
+
$_meta_value = $meta_value;
$meta_value = maybe_serialize( $meta_value );
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int $object_id ID of the object metadata is for
* @param string $meta_key Metadata key
- * @param string $meta_value Optional. Metadata value. If specified, only delete metadata entries
- * with this value. Otherwise, delete all entries with the specified meta_key.
- * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
- * for all objects, ignoring the specified object_id. Otherwise, only delete matching
+ * @param string $meta_value Optional. Metadata value. If specified, only delete metadata entries
+ * with this value. Otherwise, delete all entries with the specified meta_key.
+ * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
+ * for all objects, ignoring the specified object_id. Otherwise, only delete matching
* metadata entries for the specified object_id.
* @return bool True on successful delete, false on failure.
*/
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 );
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 );
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
* @param int $object_id ID of the object metadata is for
- * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
+ * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
* the specified object.
- * @param bool $single Optional, default is false. If true, return only the first value of the
- * specified meta_key. This parameter has no effect if meta_key is not specified.
+ * @param bool $single Optional, default is false. If true, return only the first value of the
+ * specified meta_key. This parameter has no effect if meta_key is not specified.
* @return string|array Single metadata value, or array of values
*/
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
- if ( 'post' == $meta_type )
- do_action( 'delete_postmeta', $meta_id );
+ // Old-style action.
+ if ( 'post' == $meta_type || 'comment' == $meta_type )
+ do_action( "delete_{$meta_type}meta", $meta_id );
// Run the query, will return true if deleted, false otherwise
- $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) );
+ $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
// Clear the caches.
wp_cache_delete($object_id, $meta_type . '_meta');
do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
- if ( 'post' == $meta_type )
- do_action( 'deleted_postmeta', $meta_id );
+ // Old-style action.
+ if ( 'post' == $meta_type || 'comment' == $meta_type )
+ do_action( "deleted_{$meta_type}meta", $meta_id );
return $result;
$join = array();
$where = array();
- foreach ( $this->queries as $k => $q ) {
+ $key_only_queries = array();
+ $queries = array();
+
+ // Split out the meta_key only queries (we can only do this for OR)
+ if ( 'OR' == $this->relation ) {
+ foreach ( $this->queries as $k => $q ) {
+ if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
+ $key_only_queries[$k] = $q;
+ else
+ $queries[$k] = $q;
+ }
+ } else {
+ $queries = $this->queries;
+ }
+
+ // Specify all the meta_key only queries in one go
+ if ( $key_only_queries ) {
+ $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
+
+ foreach ( $key_only_queries as $key => $q )
+ $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
+ }
+
+ foreach ( $queries as $k => $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
$meta_type = 'CHAR';
+ $meta_value = isset( $q['value'] ) ? $q['value'] : null;
+
+ if ( isset( $q['compare'] ) )
+ $meta_compare = strtoupper( $q['compare'] );
+ else
+ $meta_compare = is_array( $meta_value ) ? 'IN' : '=';
+
+ if ( ! in_array( $meta_compare, array(
+ '=', '!=', '>', '>=', '<', '<=',
+ 'LIKE', 'NOT LIKE',
+ 'IN', 'NOT IN',
+ 'BETWEEN', 'NOT BETWEEN',
+ 'NOT EXISTS'
+ ) ) )
+ $meta_compare = '=';
+
$i = count( $join );
$alias = $i ? 'mt' . $i : $meta_table;
- // Set JOIN
+ if ( 'NOT EXISTS' == $meta_compare ) {
+ $join[$i] = "LEFT JOIN $meta_table";
+ $join[$i] .= $i ? " AS $alias" : '';
+ $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
+
+ $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
+
+ continue;
+ }
+
$join[$i] = "INNER JOIN $meta_table";
$join[$i] .= $i ? " AS $alias" : '';
$join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
if ( !empty( $meta_key ) )
$where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
- if ( !isset( $q['value'] ) ) {
+ if ( is_null( $meta_value ) ) {
if ( empty( $where[$k] ) )
unset( $join[$i] );
continue;
}
- $meta_value = $q['value'];
-
- $meta_compare = is_array( $meta_value ) ? 'IN' : '=';
- if ( isset( $q['compare'] ) )
- $meta_compare = strtoupper( $q['compare'] );
-
- if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
- $meta_compare = '=';
-
if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
if ( ! is_array( $meta_value ) )
$meta_value = preg_split( '/[,\s]+/', $meta_value );
if ( is_callable( $auth_callback ) )
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
}
-
-?>