5 * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata
6 * for an object is a represented by a simple key-value pair. Objects may contain multiple
7 * metadata entries that share the same key and differ only in their value.
15 * Add metadata for the specified object.
19 * @global wpdb $wpdb WordPress database abstraction object.
21 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
22 * @param int $object_id ID of the object metadata is for
23 * @param string $meta_key Metadata key
24 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
25 * @param bool $unique Optional, default is false.
26 * Whether the specified metadata key should be unique for the object.
27 * If true, and the object already has a value for the specified metadata key,
28 * no change will be made.
29 * @return int|false The meta ID on success, false on failure.
31 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
34 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
38 $object_id = absint( $object_id );
43 $table = _get_meta_table( $meta_type );
48 $column = sanitize_key($meta_type . '_id');
50 // expected_slashed ($meta_key)
51 $meta_key = wp_unslash($meta_key);
52 $meta_value = wp_unslash($meta_value);
53 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
56 * Filter whether to add metadata of a specific type.
58 * The dynamic portion of the hook, `$meta_type`, refers to the meta
59 * object type (comment, post, or user). Returning a non-null value
60 * will effectively short-circuit the function.
64 * @param null|bool $check Whether to allow adding metadata for the given type.
65 * @param int $object_id Object ID.
66 * @param string $meta_key Meta key.
67 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
68 * @param bool $unique Whether the specified meta key should be unique
69 * for the object. Optional. Default false.
71 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
72 if ( null !== $check )
75 if ( $unique && $wpdb->get_var( $wpdb->prepare(
76 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
77 $meta_key, $object_id ) ) )
80 $_meta_value = $meta_value;
81 $meta_value = maybe_serialize( $meta_value );
84 * Fires immediately before meta of a specific type is added.
86 * The dynamic portion of the hook, `$meta_type`, refers to the meta
87 * object type (comment, post, or user).
91 * @param int $object_id Object ID.
92 * @param string $meta_key Meta key.
93 * @param mixed $meta_value Meta value.
95 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
97 $result = $wpdb->insert( $table, array(
98 $column => $object_id,
99 'meta_key' => $meta_key,
100 'meta_value' => $meta_value
106 $mid = (int) $wpdb->insert_id;
108 wp_cache_delete($object_id, $meta_type . '_meta');
111 * Fires immediately after meta of a specific type is added.
113 * The dynamic portion of the hook, `$meta_type`, refers to the meta
114 * object type (comment, post, or user).
118 * @param int $mid The meta ID after successful update.
119 * @param int $object_id Object ID.
120 * @param string $meta_key Meta key.
121 * @param mixed $meta_value Meta value.
123 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
129 * Update metadata for the specified object. If no value already exists for the specified object
130 * ID and metadata key, the metadata will be added.
134 * @global wpdb $wpdb WordPress database abstraction object.
136 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
137 * @param int $object_id ID of the object metadata is for
138 * @param string $meta_key Metadata key
139 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
140 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with
141 * the specified value. Otherwise, update all entries.
142 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
144 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
147 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
151 $object_id = absint( $object_id );
152 if ( ! $object_id ) {
156 $table = _get_meta_table( $meta_type );
161 $column = sanitize_key($meta_type . '_id');
162 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
164 // expected_slashed ($meta_key)
165 $meta_key = wp_unslash($meta_key);
166 $passed_value = $meta_value;
167 $meta_value = wp_unslash($meta_value);
168 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
171 * Filter whether to update metadata of a specific type.
173 * The dynamic portion of the hook, `$meta_type`, refers to the meta
174 * object type (comment, post, or user). Returning a non-null value
175 * will effectively short-circuit the function.
179 * @param null|bool $check Whether to allow updating metadata for the given type.
180 * @param int $object_id Object ID.
181 * @param string $meta_key Meta key.
182 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
183 * @param mixed $prev_value Optional. If specified, only update existing
184 * metadata entries with the specified value.
185 * Otherwise, update all entries.
187 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
188 if ( null !== $check )
189 return (bool) $check;
191 // Compare existing value to new value if no prev value given and the key exists only once.
192 if ( empty($prev_value) ) {
193 $old_value = get_metadata($meta_type, $object_id, $meta_key);
194 if ( count($old_value) == 1 ) {
195 if ( $old_value[0] === $meta_value )
200 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
201 if ( empty( $meta_ids ) ) {
202 return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
205 $_meta_value = $meta_value;
206 $meta_value = maybe_serialize( $meta_value );
208 $data = compact( 'meta_value' );
209 $where = array( $column => $object_id, 'meta_key' => $meta_key );
211 if ( !empty( $prev_value ) ) {
212 $prev_value = maybe_serialize($prev_value);
213 $where['meta_value'] = $prev_value;
216 foreach ( $meta_ids as $meta_id ) {
218 * Fires immediately before updating metadata of a specific type.
220 * The dynamic portion of the hook, `$meta_type`, refers to the meta
221 * object type (comment, post, or user).
225 * @param int $meta_id ID of the metadata entry to update.
226 * @param int $object_id Object ID.
227 * @param string $meta_key Meta key.
228 * @param mixed $meta_value Meta value.
230 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
233 if ( 'post' == $meta_type ) {
234 foreach ( $meta_ids as $meta_id ) {
236 * Fires immediately before updating a post's metadata.
240 * @param int $meta_id ID of metadata entry to update.
241 * @param int $object_id Object ID.
242 * @param string $meta_key Meta key.
243 * @param mixed $meta_value Meta value.
245 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
249 $result = $wpdb->update( $table, $data, $where );
253 wp_cache_delete($object_id, $meta_type . '_meta');
255 foreach ( $meta_ids as $meta_id ) {
257 * Fires immediately after updating metadata of a specific type.
259 * The dynamic portion of the hook, `$meta_type`, refers to the meta
260 * object type (comment, post, or user).
264 * @param int $meta_id ID of updated metadata entry.
265 * @param int $object_id Object ID.
266 * @param string $meta_key Meta key.
267 * @param mixed $meta_value Meta value.
269 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
272 if ( 'post' == $meta_type ) {
273 foreach ( $meta_ids as $meta_id ) {
275 * Fires immediately after updating a post's metadata.
279 * @param int $meta_id ID of updated metadata entry.
280 * @param int $object_id Object ID.
281 * @param string $meta_key Meta key.
282 * @param mixed $meta_value Meta value.
284 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
292 * Delete metadata for the specified object.
296 * @global wpdb $wpdb WordPress database abstraction object.
298 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
299 * @param int $object_id ID of the object metadata is for
300 * @param string $meta_key Metadata key
301 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
302 * metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
303 * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility,
304 * it is not possible to pass an empty string to delete those entries with an empty string
306 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
307 * ignoring the specified object_id. Otherwise, only delete matching metadata entries for
308 * the specified object_id.
309 * @return bool True on successful delete, false on failure.
311 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
314 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
318 $object_id = absint( $object_id );
319 if ( ! $object_id && ! $delete_all ) {
323 $table = _get_meta_table( $meta_type );
328 $type_column = sanitize_key($meta_type . '_id');
329 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
330 // expected_slashed ($meta_key)
331 $meta_key = wp_unslash($meta_key);
332 $meta_value = wp_unslash($meta_value);
335 * Filter whether to delete metadata of a specific type.
337 * The dynamic portion of the hook, `$meta_type`, refers to the meta
338 * object type (comment, post, or user). Returning a non-null value
339 * will effectively short-circuit the function.
343 * @param null|bool $delete Whether to allow metadata deletion of the given type.
344 * @param int $object_id Object ID.
345 * @param string $meta_key Meta key.
346 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
347 * @param bool $delete_all Whether to delete the matching metadata entries
348 * for all objects, ignoring the specified $object_id.
351 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
352 if ( null !== $check )
353 return (bool) $check;
355 $_meta_value = $meta_value;
356 $meta_value = maybe_serialize( $meta_value );
358 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
361 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
363 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value )
364 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
366 $meta_ids = $wpdb->get_col( $query );
367 if ( !count( $meta_ids ) )
371 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
374 * Fires immediately before deleting metadata of a specific type.
376 * The dynamic portion of the hook, `$meta_type`, refers to the meta
377 * object type (comment, post, or user).
381 * @param array $meta_ids An array of metadata entry IDs to delete.
382 * @param int $object_id Object ID.
383 * @param string $meta_key Meta key.
384 * @param mixed $meta_value Meta value.
386 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
389 if ( 'post' == $meta_type ) {
391 * Fires immediately before deleting metadata for a post.
395 * @param array $meta_ids An array of post metadata entry IDs to delete.
397 do_action( 'delete_postmeta', $meta_ids );
400 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
402 $count = $wpdb->query($query);
408 foreach ( (array) $object_ids as $o_id ) {
409 wp_cache_delete($o_id, $meta_type . '_meta');
412 wp_cache_delete($object_id, $meta_type . '_meta');
416 * Fires immediately after deleting metadata of a specific type.
418 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
419 * object type (comment, post, or user).
423 * @param array $meta_ids An array of deleted metadata entry IDs.
424 * @param int $object_id Object ID.
425 * @param string $meta_key Meta key.
426 * @param mixed $meta_value Meta value.
428 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
431 if ( 'post' == $meta_type ) {
433 * Fires immediately after deleting metadata for a post.
437 * @param array $meta_ids An array of deleted post metadata entry IDs.
439 do_action( 'deleted_postmeta', $meta_ids );
446 * Retrieve metadata for the specified object.
450 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
451 * @param int $object_id ID of the object metadata is for
452 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
453 * the specified object.
454 * @param bool $single Optional, default is false.
455 * If true, return only the first value of the specified meta_key.
456 * This parameter has no effect if meta_key is not specified.
457 * @return mixed Single metadata value, or array of values
459 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
460 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
464 $object_id = absint( $object_id );
465 if ( ! $object_id ) {
470 * Filter whether to retrieve metadata of a specific type.
472 * The dynamic portion of the hook, `$meta_type`, refers to the meta
473 * object type (comment, post, or user). Returning a non-null value
474 * will effectively short-circuit the function.
478 * @param null|array|string $value The value get_metadata() should
479 * return - a single metadata value,
480 * or an array of values.
481 * @param int $object_id Object ID.
482 * @param string $meta_key Meta key.
483 * @param string|array $single Meta value, or an array of values.
485 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
486 if ( null !== $check ) {
487 if ( $single && is_array( $check ) )
493 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
495 if ( !$meta_cache ) {
496 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
497 $meta_cache = $meta_cache[$object_id];
504 if ( isset($meta_cache[$meta_key]) ) {
506 return maybe_unserialize( $meta_cache[$meta_key][0] );
508 return array_map('maybe_unserialize', $meta_cache[$meta_key]);
518 * Determine if a meta key is set for a given object
522 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
523 * @param int $object_id ID of the object metadata is for
524 * @param string $meta_key Metadata key.
525 * @return bool True of the key is set, false if not.
527 function metadata_exists( $meta_type, $object_id, $meta_key ) {
528 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
532 $object_id = absint( $object_id );
533 if ( ! $object_id ) {
537 /** This filter is documented in wp-includes/meta.php */
538 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
539 if ( null !== $check )
540 return (bool) $check;
542 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
544 if ( !$meta_cache ) {
545 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
546 $meta_cache = $meta_cache[$object_id];
549 if ( isset( $meta_cache[ $meta_key ] ) )
556 * Get meta data by meta ID
562 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
563 * @param int $meta_id ID for a specific meta row
564 * @return object|false Meta object or false.
566 function get_metadata_by_mid( $meta_type, $meta_id ) {
569 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
573 $meta_id = absint( $meta_id );
578 $table = _get_meta_table( $meta_type );
583 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
585 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
587 if ( empty( $meta ) )
590 if ( isset( $meta->meta_value ) )
591 $meta->meta_value = maybe_unserialize( $meta->meta_value );
597 * Update meta data by meta ID
603 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
604 * @param int $meta_id ID for a specific meta row
605 * @param string $meta_value Metadata value
606 * @param string $meta_key Optional, you can provide a meta key to update it
607 * @return bool True on successful update, false on failure.
609 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
612 // Make sure everything is valid.
613 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
617 $meta_id = absint( $meta_id );
622 $table = _get_meta_table( $meta_type );
627 $column = sanitize_key($meta_type . '_id');
628 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
630 // Fetch the meta and go on if it's found.
631 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
632 $original_key = $meta->meta_key;
633 $object_id = $meta->{$column};
635 // If a new meta_key (last parameter) was specified, change the meta key,
636 // otherwise use the original key in the update statement.
637 if ( false === $meta_key ) {
638 $meta_key = $original_key;
639 } elseif ( ! is_string( $meta_key ) ) {
644 $_meta_value = $meta_value;
645 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
646 $meta_value = maybe_serialize( $meta_value );
648 // Format the data query arguments.
650 'meta_key' => $meta_key,
651 'meta_value' => $meta_value
654 // Format the where query arguments.
656 $where[$id_column] = $meta_id;
658 /** This action is documented in wp-includes/meta.php */
659 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
661 if ( 'post' == $meta_type ) {
662 /** This action is documented in wp-includes/meta.php */
663 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
666 // Run the update query, all fields in $data are %s, $where is a %d.
667 $result = $wpdb->update( $table, $data, $where, '%s', '%d' );
672 wp_cache_delete($object_id, $meta_type . '_meta');
674 /** This action is documented in wp-includes/meta.php */
675 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
677 if ( 'post' == $meta_type ) {
678 /** This action is documented in wp-includes/meta.php */
679 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
685 // And if the meta was not found.
690 * Delete meta data by meta ID
696 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
697 * @param int $meta_id ID for a specific meta row
698 * @return bool True on successful delete, false on failure.
700 function delete_metadata_by_mid( $meta_type, $meta_id ) {
703 // Make sure everything is valid.
704 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
708 $meta_id = absint( $meta_id );
713 $table = _get_meta_table( $meta_type );
718 // object and id columns
719 $column = sanitize_key($meta_type . '_id');
720 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
722 // Fetch the meta and go on if it's found.
723 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
724 $object_id = $meta->{$column};
726 /** This action is documented in wp-includes/meta.php */
727 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
730 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
732 * Fires immediately before deleting post or comment metadata of a specific type.
734 * The dynamic portion of the hook, `$meta_type`, refers to the meta
735 * object type (post or comment).
739 * @param int $meta_id ID of the metadata entry to delete.
741 do_action( "delete_{$meta_type}meta", $meta_id );
744 // Run the query, will return true if deleted, false otherwise
745 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
748 wp_cache_delete($object_id, $meta_type . '_meta');
750 /** This action is documented in wp-includes/meta.php */
751 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
754 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
756 * Fires immediately after deleting post or comment metadata of a specific type.
758 * The dynamic portion of the hook, `$meta_type`, refers to the meta
759 * object type (post or comment).
763 * @param int $meta_ids Deleted metadata entry ID.
765 do_action( "deleted_{$meta_type}meta", $meta_id );
772 // Meta id was not found.
777 * Update the metadata cache for the specified objects.
781 * @global wpdb $wpdb WordPress database abstraction object.
783 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
784 * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
785 * @return array|false Metadata cache for the specified objects, or false on failure.
787 function update_meta_cache($meta_type, $object_ids) {
790 if ( ! $meta_type || ! $object_ids ) {
794 $table = _get_meta_table( $meta_type );
799 $column = sanitize_key($meta_type . '_id');
801 if ( !is_array($object_ids) ) {
802 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
803 $object_ids = explode(',', $object_ids);
806 $object_ids = array_map('intval', $object_ids);
808 $cache_key = $meta_type . '_meta';
811 foreach ( $object_ids as $id ) {
812 $cached_object = wp_cache_get( $id, $cache_key );
813 if ( false === $cached_object )
816 $cache[$id] = $cached_object;
823 $id_list = join( ',', $ids );
824 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
825 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );
827 if ( !empty($meta_list) ) {
828 foreach ( $meta_list as $metarow) {
829 $mpid = intval($metarow[$column]);
830 $mkey = $metarow['meta_key'];
831 $mval = $metarow['meta_value'];
833 // Force subkeys to be array type:
834 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
835 $cache[$mpid] = array();
836 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
837 $cache[$mpid][$mkey] = array();
839 // Add a value to the current pid/key:
840 $cache[$mpid][$mkey][] = $mval;
844 foreach ( $ids as $id ) {
845 if ( ! isset($cache[$id]) )
846 $cache[$id] = array();
847 wp_cache_add( $id, $cache[$id], $cache_key );
854 * Given a meta query, generates SQL clauses to be appended to a main query.
860 * @param array $meta_query A meta query.
861 * @param string $type Type of meta.
862 * @param string $primary_table Primary database table name.
863 * @param string $primary_id_column Primary ID column name.
864 * @param object $context Optional. The main query object
865 * @return array Associative array of `JOIN` and `WHERE` SQL.
867 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
868 $meta_query_obj = new WP_Meta_Query( $meta_query );
869 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
873 * Class for generating SQL clauses that filter a primary query according to metadata keys and values.
875 * `WP_Meta_Query` is a helper that allows primary query classes, such as {@see WP_Query} and {@see WP_User_Query},
876 * to filter their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
877 * to the primary SQL query string.
881 class WP_Meta_Query {
883 * Array of metadata queries.
885 * See {@see WP_Meta_Query::__construct()} for information on meta query arguments.
891 public $queries = array();
894 * The relation between the queries. Can be one of 'AND' or 'OR'.
903 * Database table to query for the metadata.
912 * Column in meta_table that represents the ID of the object the metadata belongs to.
918 public $meta_id_column;
921 * Database table that where the metadata's objects are stored (eg $wpdb->users).
927 public $primary_table;
930 * Column in primary_table that represents the ID of the object.
936 public $primary_id_column;
939 * A flat list of table aliases used in JOIN clauses.
945 protected $table_aliases = array();
948 * A flat list of clauses, keyed by clause 'name'.
954 protected $clauses = array();
957 * Whether the query contains any OR relations.
963 protected $has_or_relation = false;
969 * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
973 * @param array $meta_query {
974 * Array of meta query clauses. When first-order clauses use strings as their array keys, they may be
975 * referenced in the 'orderby' parameter of the parent query.
977 * @type string $relation Optional. The MySQL keyword used to join
978 * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
980 * Optional. An array of first-order clause parameters, or another fully-formed meta query.
982 * @type string $key Meta key to filter by.
983 * @type string $value Meta value to filter by.
984 * @type string $compare MySQL operator used for comparing the $value. Accepts '=',
985 * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
986 * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', or 'RLIKE'.
987 * Default is 'IN' when `$value` is an array, '=' otherwise.
988 * @type string $type MySQL data type that the meta_value column will be CAST to for
989 * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
990 * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'.
995 public function __construct( $meta_query = false ) {
999 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
1000 $this->relation = 'OR';
1002 $this->relation = 'AND';
1005 $this->queries = $this->sanitize_query( $meta_query );
1009 * Ensure the 'meta_query' argument passed to the class constructor is well-formed.
1011 * Eliminates empty items and ensures that a 'relation' is set.
1016 * @param array $queries Array of query clauses.
1017 * @return array Sanitized array of query clauses.
1019 public function sanitize_query( $queries ) {
1020 $clean_queries = array();
1022 if ( ! is_array( $queries ) ) {
1023 return $clean_queries;
1026 foreach ( $queries as $key => $query ) {
1027 if ( 'relation' === $key ) {
1030 } elseif ( ! is_array( $query ) ) {
1033 // First-order clause.
1034 } elseif ( $this->is_first_order_clause( $query ) ) {
1035 if ( isset( $query['value'] ) && array() === $query['value'] ) {
1036 unset( $query['value'] );
1039 $clean_queries[ $key ] = $query;
1041 // Otherwise, it's a nested query, so we recurse.
1043 $cleaned_query = $this->sanitize_query( $query );
1045 if ( ! empty( $cleaned_query ) ) {
1046 $clean_queries[ $key ] = $cleaned_query;
1051 if ( empty( $clean_queries ) ) {
1052 return $clean_queries;
1055 // Sanitize the 'relation' key provided in the query.
1056 if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
1057 $clean_queries['relation'] = 'OR';
1058 $this->has_or_relation = true;
1061 * If there is only a single clause, call the relation 'OR'.
1062 * This value will not actually be used to join clauses, but it
1063 * simplifies the logic around combining key-only queries.
1065 } elseif ( 1 === count( $clean_queries ) ) {
1066 $clean_queries['relation'] = 'OR';
1070 $clean_queries['relation'] = 'AND';
1073 return $clean_queries;
1077 * Determine whether a query clause is first-order.
1079 * A first-order meta query clause is one that has either a 'key' or
1080 * a 'value' array key.
1085 * @param array $query Meta query arguments.
1086 * @return bool Whether the query clause is a first-order clause.
1088 protected function is_first_order_clause( $query ) {
1089 return isset( $query['key'] ) || isset( $query['value'] );
1093 * Constructs a meta query based on 'meta_*' query vars
1098 * @param array $qv The query variables
1100 public function parse_query_vars( $qv ) {
1101 $meta_query = array();
1104 * For orderby=meta_value to work correctly, simple query needs to be
1105 * first (so that its table join is against an unaliased meta table) and
1106 * needs to be its own clause (so it doesn't interfere with the logic of
1107 * the rest of the meta_query).
1109 $primary_meta_query = array();
1110 foreach ( array( 'key', 'compare', 'type' ) as $key ) {
1111 if ( ! empty( $qv[ "meta_$key" ] ) ) {
1112 $primary_meta_query[ $key ] = $qv[ "meta_$key" ];
1116 // WP_Query sets 'meta_value' = '' by default.
1117 if ( isset( $qv['meta_value'] ) && '' !== $qv['meta_value'] && ( ! is_array( $qv['meta_value'] ) || $qv['meta_value'] ) ) {
1118 $primary_meta_query['value'] = $qv['meta_value'];
1121 $existing_meta_query = isset( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ? $qv['meta_query'] : array();
1123 if ( ! empty( $primary_meta_query ) && ! empty( $existing_meta_query ) ) {
1124 $meta_query = array(
1125 'relation' => 'AND',
1126 $primary_meta_query,
1127 $existing_meta_query,
1129 } elseif ( ! empty( $primary_meta_query ) ) {
1130 $meta_query = array(
1131 $primary_meta_query,
1133 } elseif ( ! empty( $existing_meta_query ) ) {
1134 $meta_query = $existing_meta_query;
1137 $this->__construct( $meta_query );
1141 * Return the appropriate alias for the given meta type if applicable.
1146 * @param string $type MySQL type to cast meta_value.
1147 * @return string MySQL type.
1149 public function get_cast_for_type( $type = '' ) {
1150 if ( empty( $type ) )
1153 $meta_type = strtoupper( $type );
1155 if ( ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) )
1158 if ( 'NUMERIC' == $meta_type )
1159 $meta_type = 'SIGNED';
1165 * Generates SQL clauses to be appended to a main query.
1170 * @param string $type Type of meta, eg 'user', 'post'.
1171 * @param string $primary_table Database table where the object being filtered is stored (eg wp_users).
1172 * @param string $primary_id_column ID column for the filtered object in $primary_table.
1173 * @param object $context Optional. The main query object.
1174 * @return false|array {
1175 * Array containing JOIN and WHERE SQL clauses to append to the main query.
1177 * @type string $join SQL fragment to append to the main JOIN clause.
1178 * @type string $where SQL fragment to append to the main WHERE clause.
1181 public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
1182 if ( ! $meta_table = _get_meta_table( $type ) ) {
1186 $this->meta_table = $meta_table;
1187 $this->meta_id_column = sanitize_key( $type . '_id' );
1189 $this->primary_table = $primary_table;
1190 $this->primary_id_column = $primary_id_column;
1192 $sql = $this->get_sql_clauses();
1195 * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
1196 * be LEFT. Otherwise posts with no metadata will be excluded from results.
1198 if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
1199 $sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
1203 * Filter the meta query's generated SQL.
1207 * @param array $args {
1208 * An array of meta query SQL arguments.
1210 * @type array $clauses Array containing the query's JOIN and WHERE clauses.
1211 * @type array $queries Array of meta queries.
1212 * @type string $type Type of meta.
1213 * @type string $primary_table Primary table.
1214 * @type string $primary_id_column Primary column ID.
1215 * @type object $context The main query object.
1218 return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
1222 * Generate SQL clauses to be appended to a main query.
1224 * Called by the public {@see WP_Meta_Query::get_sql()}, this method
1225 * is abstracted out to maintain parity with the other Query classes.
1231 * Array containing JOIN and WHERE SQL clauses to append to the main query.
1233 * @type string $join SQL fragment to append to the main JOIN clause.
1234 * @type string $where SQL fragment to append to the main WHERE clause.
1237 protected function get_sql_clauses() {
1239 * $queries are passed by reference to get_sql_for_query() for recursion.
1240 * To keep $this->queries unaltered, pass a copy.
1242 $queries = $this->queries;
1243 $sql = $this->get_sql_for_query( $queries );
1245 if ( ! empty( $sql['where'] ) ) {
1246 $sql['where'] = ' AND ' . $sql['where'];
1253 * Generate SQL clauses for a single query array.
1255 * If nested subqueries are found, this method recurses the tree to
1256 * produce the properly nested SQL.
1261 * @param array $query Query to parse, passed by reference.
1262 * @param int $depth Optional. Number of tree levels deep we currently are.
1263 * Used to calculate indentation. Default 0.
1265 * Array containing JOIN and WHERE SQL clauses to append to a single query array.
1267 * @type string $join SQL fragment to append to the main JOIN clause.
1268 * @type string $where SQL fragment to append to the main WHERE clause.
1271 protected function get_sql_for_query( &$query, $depth = 0 ) {
1272 $sql_chunks = array(
1283 for ( $i = 0; $i < $depth; $i++ ) {
1287 foreach ( $query as $key => &$clause ) {
1288 if ( 'relation' === $key ) {
1289 $relation = $query['relation'];
1290 } elseif ( is_array( $clause ) ) {
1292 // This is a first-order clause.
1293 if ( $this->is_first_order_clause( $clause ) ) {
1294 $clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
1296 $where_count = count( $clause_sql['where'] );
1297 if ( ! $where_count ) {
1298 $sql_chunks['where'][] = '';
1299 } elseif ( 1 === $where_count ) {
1300 $sql_chunks['where'][] = $clause_sql['where'][0];
1302 $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
1305 $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
1306 // This is a subquery, so we recurse.
1308 $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
1310 $sql_chunks['where'][] = $clause_sql['where'];
1311 $sql_chunks['join'][] = $clause_sql['join'];
1316 // Filter to remove empties.
1317 $sql_chunks['join'] = array_filter( $sql_chunks['join'] );
1318 $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
1320 if ( empty( $relation ) ) {
1324 // Filter duplicate JOIN clauses and combine into a single string.
1325 if ( ! empty( $sql_chunks['join'] ) ) {
1326 $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
1329 // Generate a single WHERE clause with proper brackets and indentation.
1330 if ( ! empty( $sql_chunks['where'] ) ) {
1331 $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
1338 * Generate SQL JOIN and WHERE clauses for a first-order query clause.
1340 * "First-order" means that it's an array with a 'key' or 'value'.
1345 * @global wpdb $wpdb
1347 * @param array $clause Query clause, passed by reference.
1348 * @param array $parent_query Parent query array.
1349 * @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
1350 * parameters. If not provided, a key will be generated automatically.
1352 * Array containing JOIN and WHERE SQL clauses to append to a first-order query.
1354 * @type string $join SQL fragment to append to the main JOIN clause.
1355 * @type string $where SQL fragment to append to the main WHERE clause.
1358 public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
1361 $sql_chunks = array(
1366 if ( isset( $clause['compare'] ) ) {
1367 $clause['compare'] = strtoupper( $clause['compare'] );
1369 $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
1372 if ( ! in_array( $clause['compare'], array(
1373 '=', '!=', '>', '>=', '<', '<=',
1376 'BETWEEN', 'NOT BETWEEN',
1377 'EXISTS', 'NOT EXISTS',
1378 'REGEXP', 'NOT REGEXP', 'RLIKE'
1380 $clause['compare'] = '=';
1383 $meta_compare = $clause['compare'];
1385 // First build the JOIN clause, if one is required.
1388 // We prefer to avoid joins if possible. Look for an existing join compatible with this clause.
1389 $alias = $this->find_compatible_table_alias( $clause, $parent_query );
1390 if ( false === $alias ) {
1391 $i = count( $this->table_aliases );
1392 $alias = $i ? 'mt' . $i : $this->meta_table;
1394 // JOIN clauses for NOT EXISTS have their own syntax.
1395 if ( 'NOT EXISTS' === $meta_compare ) {
1396 $join .= " LEFT JOIN $this->meta_table";
1397 $join .= $i ? " AS $alias" : '';
1398 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
1400 // All other JOIN clauses.
1402 $join .= " INNER JOIN $this->meta_table";
1403 $join .= $i ? " AS $alias" : '';
1404 $join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";
1407 $this->table_aliases[] = $alias;
1408 $sql_chunks['join'][] = $join;
1411 // Save the alias to this clause, for future siblings to find.
1412 $clause['alias'] = $alias;
1414 // Determine the data type.
1415 $_meta_type = isset( $clause['type'] ) ? $clause['type'] : '';
1416 $meta_type = $this->get_cast_for_type( $_meta_type );
1417 $clause['cast'] = $meta_type;
1419 // Fallback for clause keys is the table alias.
1420 if ( ! $clause_key ) {
1421 $clause_key = $clause['alias'];
1424 // Ensure unique clause keys, so none are overwritten.
1426 $clause_key_base = $clause_key;
1427 while ( isset( $this->clauses[ $clause_key ] ) ) {
1428 $clause_key = $clause_key_base . '-' . $iterator;
1432 // Store the clause in our flat array.
1433 $this->clauses[ $clause_key ] =& $clause;
1435 // Next, build the WHERE clause.
1438 if ( array_key_exists( 'key', $clause ) ) {
1439 if ( 'NOT EXISTS' === $meta_compare ) {
1440 $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
1442 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
1447 if ( array_key_exists( 'value', $clause ) ) {
1448 $meta_value = $clause['value'];
1450 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
1451 if ( ! is_array( $meta_value ) ) {
1452 $meta_value = preg_split( '/[,\s]+/', $meta_value );
1455 $meta_value = trim( $meta_value );
1458 switch ( $meta_compare ) {
1461 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
1462 $where = $wpdb->prepare( $meta_compare_string, $meta_value );
1466 case 'NOT BETWEEN' :
1467 $meta_value = array_slice( $meta_value, 0, 2 );
1468 $where = $wpdb->prepare( '%s AND %s', $meta_value );
1473 $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
1474 $where = $wpdb->prepare( '%s', $meta_value );
1477 // EXISTS with a value is interpreted as '='.
1479 $meta_compare = '=';
1480 $where = $wpdb->prepare( '%s', $meta_value );
1483 // 'value' is ignored for NOT EXISTS.
1489 $where = $wpdb->prepare( '%s', $meta_value );
1495 $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
1500 * Multiple WHERE clauses (for meta_key and meta_value) should
1501 * be joined in parentheses.
1503 if ( 1 < count( $sql_chunks['where'] ) ) {
1504 $sql_chunks['where'] = array( '( ' . implode( ' AND ', $sql_chunks['where'] ) . ' )' );
1511 * Get a flattened list of sanitized meta clauses.
1513 * This array should be used for clause lookup, as when the table alias and CAST type must be determined for
1514 * a value of 'orderby' corresponding to a meta clause.
1519 * @return array Meta clauses.
1521 public function get_clauses() {
1522 return $this->clauses;
1526 * Identify an existing table alias that is compatible with the current
1529 * We avoid unnecessary table joins by allowing each clause to look for
1530 * an existing table alias that is compatible with the query that it
1533 * An existing alias is compatible if (a) it is a sibling of `$clause`
1534 * (ie, it's under the scope of the same relation), and (b) the combination
1535 * of operator and relation between the clauses allows for a shared table join.
1536 * In the case of {@see WP_Meta_Query}, this only applies to 'IN' clauses that
1537 * are connected by the relation 'OR'.
1542 * @param array $clause Query clause.
1543 * @param array $parent_query Parent query of $clause.
1544 * @return string|bool Table alias if found, otherwise false.
1546 protected function find_compatible_table_alias( $clause, $parent_query ) {
1549 foreach ( $parent_query as $sibling ) {
1550 // If the sibling has no alias yet, there's nothing to check.
1551 if ( empty( $sibling['alias'] ) ) {
1555 // We're only interested in siblings that are first-order clauses.
1556 if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
1560 $compatible_compares = array();
1562 // Clauses connected by OR can share joins as long as they have "positive" operators.
1563 if ( 'OR' === $parent_query['relation'] ) {
1564 $compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' );
1566 // Clauses joined by AND with "negative" operators share a join only if they also share a key.
1567 } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) {
1568 $compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );
1571 $clause_compare = strtoupper( $clause['compare'] );
1572 $sibling_compare = strtoupper( $sibling['compare'] );
1573 if ( in_array( $clause_compare, $compatible_compares ) && in_array( $sibling_compare, $compatible_compares ) ) {
1574 $alias = $sibling['alias'];
1580 * Filter the table alias identified as compatible with the current clause.
1584 * @param string|bool $alias Table alias, or false if none was found.
1585 * @param array $clause First-order query clause.
1586 * @param array $parent_query Parent of $clause.
1587 * @param object $this WP_Meta_Query object.
1589 return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
1593 * Checks whether the current query has any OR relations.
1595 * In some cases, the presence of an OR relation somewhere in the query will require
1596 * the use of a `DISTINCT` or `GROUP BY` keyword in the `SELECT` clause. The current
1597 * method can be used in these cases to determine whether such a clause is necessary.
1601 * @return bool True if the query contains any `OR` relations, otherwise false.
1603 public function has_or_relation() {
1604 return $this->has_or_relation;
1609 * Retrieve the name of the metadata table for the specified object type.
1613 * @global wpdb $wpdb WordPress database abstraction object.
1615 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
1616 * @return string|false Metadata table name, or false if no metadata table exists
1618 function _get_meta_table($type) {
1621 $table_name = $type . 'meta';
1623 if ( empty($wpdb->$table_name) )
1626 return $wpdb->$table_name;
1630 * Determine whether a meta key is protected.
1634 * @param string $meta_key Meta key
1635 * @param string|null $meta_type
1636 * @return bool True if the key is protected, false otherwise.
1638 function is_protected_meta( $meta_key, $meta_type = null ) {
1639 $protected = ( '_' == $meta_key[0] );
1642 * Filter whether a meta key is protected.
1646 * @param bool $protected Whether the key is protected. Default false.
1647 * @param string $meta_key Meta key.
1648 * @param string $meta_type Meta type.
1650 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
1654 * Sanitize meta value.
1658 * @param string $meta_key Meta key
1659 * @param mixed $meta_value Meta value to sanitize
1660 * @param string $meta_type Type of meta
1661 * @return mixed Sanitized $meta_value
1663 function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
1666 * Filter the sanitization of a specific meta key of a specific meta type.
1668 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
1669 * refer to the metadata object type (comment, post, or user) and the meta
1675 * @param mixed $meta_value Meta value to sanitize.
1676 * @param string $meta_key Meta key.
1677 * @param string $meta_type Meta type.
1679 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
1687 * @param string $meta_type Type of meta
1688 * @param string $meta_key Meta key
1689 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
1690 * @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
1692 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
1693 if ( is_callable( $sanitize_callback ) )
1694 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
1696 if ( empty( $auth_callback ) ) {
1697 if ( is_protected_meta( $meta_key, $meta_type ) )
1698 $auth_callback = '__return_false';
1700 $auth_callback = '__return_true';
1703 if ( is_callable( $auth_callback ) )
1704 add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );