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.
14 * Add metadata for the specified object.
18 * @global wpdb $wpdb WordPress database abstraction object.
20 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
21 * @param int $object_id ID of the object metadata is for
22 * @param string $meta_key Metadata key
23 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
24 * @param bool $unique Optional, default is false.
25 * Whether the specified metadata key should be unique for the object.
26 * If true, and the object already has a value for the specified metadata key,
27 * no change will be made.
28 * @return int|false The meta ID on success, false on failure.
30 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
33 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
37 $object_id = absint( $object_id );
42 $table = _get_meta_table( $meta_type );
47 $column = sanitize_key($meta_type . '_id');
49 // expected_slashed ($meta_key)
50 $meta_key = wp_unslash($meta_key);
51 $meta_value = wp_unslash($meta_value);
52 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
55 * Filters whether to add metadata of a specific type.
57 * The dynamic portion of the hook, `$meta_type`, refers to the meta
58 * object type (comment, post, or user). Returning a non-null value
59 * will effectively short-circuit the function.
63 * @param null|bool $check Whether to allow adding metadata for the given type.
64 * @param int $object_id Object ID.
65 * @param string $meta_key Meta key.
66 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
67 * @param bool $unique Whether the specified meta key should be unique
68 * for the object. Optional. Default false.
70 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
71 if ( null !== $check )
74 if ( $unique && $wpdb->get_var( $wpdb->prepare(
75 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
76 $meta_key, $object_id ) ) )
79 $_meta_value = $meta_value;
80 $meta_value = maybe_serialize( $meta_value );
83 * Fires immediately before meta of a specific type is added.
85 * The dynamic portion of the hook, `$meta_type`, refers to the meta
86 * object type (comment, post, or user).
90 * @param int $object_id Object ID.
91 * @param string $meta_key Meta key.
92 * @param mixed $meta_value Meta value.
94 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
96 $result = $wpdb->insert( $table, array(
97 $column => $object_id,
98 'meta_key' => $meta_key,
99 'meta_value' => $meta_value
105 $mid = (int) $wpdb->insert_id;
107 wp_cache_delete($object_id, $meta_type . '_meta');
110 * Fires immediately after meta of a specific type is added.
112 * The dynamic portion of the hook, `$meta_type`, refers to the meta
113 * object type (comment, post, or user).
117 * @param int $mid The meta ID after successful update.
118 * @param int $object_id Object ID.
119 * @param string $meta_key Meta key.
120 * @param mixed $meta_value Meta value.
122 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
128 * Update metadata for the specified object. If no value already exists for the specified object
129 * ID and metadata key, the metadata will be added.
133 * @global wpdb $wpdb WordPress database abstraction object.
135 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
136 * @param int $object_id ID of the object metadata is for
137 * @param string $meta_key Metadata key
138 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
139 * @param mixed $prev_value Optional. If specified, only update existing metadata entries with
140 * the specified value. Otherwise, update all entries.
141 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
143 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
146 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
150 $object_id = absint( $object_id );
151 if ( ! $object_id ) {
155 $table = _get_meta_table( $meta_type );
160 $column = sanitize_key($meta_type . '_id');
161 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
163 // expected_slashed ($meta_key)
164 $raw_meta_key = $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 * Filters 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, $raw_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 );
232 if ( 'post' == $meta_type ) {
234 * Fires immediately before updating a post's metadata.
238 * @param int $meta_id ID of metadata entry to update.
239 * @param int $object_id Object ID.
240 * @param string $meta_key Meta key.
241 * @param mixed $meta_value Meta value.
243 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
247 $result = $wpdb->update( $table, $data, $where );
251 wp_cache_delete($object_id, $meta_type . '_meta');
253 foreach ( $meta_ids as $meta_id ) {
255 * Fires immediately after updating metadata of a specific type.
257 * The dynamic portion of the hook, `$meta_type`, refers to the meta
258 * object type (comment, post, or user).
262 * @param int $meta_id ID of updated metadata entry.
263 * @param int $object_id Object ID.
264 * @param string $meta_key Meta key.
265 * @param mixed $meta_value Meta value.
267 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
269 if ( 'post' == $meta_type ) {
271 * Fires immediately after updating a post's metadata.
275 * @param int $meta_id ID of updated metadata entry.
276 * @param int $object_id Object ID.
277 * @param string $meta_key Meta key.
278 * @param mixed $meta_value Meta value.
280 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
288 * Delete metadata for the specified object.
292 * @global wpdb $wpdb WordPress database abstraction object.
294 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
295 * @param int $object_id ID of the object metadata is for
296 * @param string $meta_key Metadata key
297 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
298 * metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
299 * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility,
300 * it is not possible to pass an empty string to delete those entries with an empty string
302 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
303 * ignoring the specified object_id. Otherwise, only delete matching metadata entries for
304 * the specified object_id.
305 * @return bool True on successful delete, false on failure.
307 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
310 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
314 $object_id = absint( $object_id );
315 if ( ! $object_id && ! $delete_all ) {
319 $table = _get_meta_table( $meta_type );
324 $type_column = sanitize_key($meta_type . '_id');
325 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
326 // expected_slashed ($meta_key)
327 $meta_key = wp_unslash($meta_key);
328 $meta_value = wp_unslash($meta_value);
331 * Filters whether to delete metadata of a specific type.
333 * The dynamic portion of the hook, `$meta_type`, refers to the meta
334 * object type (comment, post, or user). Returning a non-null value
335 * will effectively short-circuit the function.
339 * @param null|bool $delete Whether to allow metadata deletion of the given type.
340 * @param int $object_id Object ID.
341 * @param string $meta_key Meta key.
342 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
343 * @param bool $delete_all Whether to delete the matching metadata entries
344 * for all objects, ignoring the specified $object_id.
347 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
348 if ( null !== $check )
349 return (bool) $check;
351 $_meta_value = $meta_value;
352 $meta_value = maybe_serialize( $meta_value );
354 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
357 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
359 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value )
360 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
362 $meta_ids = $wpdb->get_col( $query );
363 if ( !count( $meta_ids ) )
368 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
369 $value_clause = $wpdb->prepare( " AND meta_value = %s", $meta_value );
372 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s $value_clause", $meta_key ) );
376 * Fires immediately before deleting metadata of a specific type.
378 * The dynamic portion of the hook, `$meta_type`, refers to the meta
379 * object type (comment, post, or user).
383 * @param array $meta_ids An array of metadata entry IDs to delete.
384 * @param int $object_id Object ID.
385 * @param string $meta_key Meta key.
386 * @param mixed $meta_value Meta value.
388 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
391 if ( 'post' == $meta_type ) {
393 * Fires immediately before deleting metadata for a post.
397 * @param array $meta_ids An array of post metadata entry IDs to delete.
399 do_action( 'delete_postmeta', $meta_ids );
402 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
404 $count = $wpdb->query($query);
410 foreach ( (array) $object_ids as $o_id ) {
411 wp_cache_delete($o_id, $meta_type . '_meta');
414 wp_cache_delete($object_id, $meta_type . '_meta');
418 * Fires immediately after deleting metadata of a specific type.
420 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
421 * object type (comment, post, or user).
425 * @param array $meta_ids An array of deleted metadata entry IDs.
426 * @param int $object_id Object ID.
427 * @param string $meta_key Meta key.
428 * @param mixed $meta_value Meta value.
430 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
433 if ( 'post' == $meta_type ) {
435 * Fires immediately after deleting metadata for a post.
439 * @param array $meta_ids An array of deleted post metadata entry IDs.
441 do_action( 'deleted_postmeta', $meta_ids );
448 * Retrieve metadata for the specified object.
452 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
453 * @param int $object_id ID of the object metadata is for
454 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
455 * the specified object.
456 * @param bool $single Optional, default is false.
457 * If true, return only the first value of the specified meta_key.
458 * This parameter has no effect if meta_key is not specified.
459 * @return mixed Single metadata value, or array of values
461 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
462 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
466 $object_id = absint( $object_id );
467 if ( ! $object_id ) {
472 * Filters whether to retrieve metadata of a specific type.
474 * The dynamic portion of the hook, `$meta_type`, refers to the meta
475 * object type (comment, post, or user). Returning a non-null value
476 * will effectively short-circuit the function.
480 * @param null|array|string $value The value get_metadata() should return - a single metadata value,
481 * or an array of values.
482 * @param int $object_id Object ID.
483 * @param string $meta_key Meta key.
484 * @param bool $single Whether to return only the first value of the specified $meta_key.
486 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
487 if ( null !== $check ) {
488 if ( $single && is_array( $check ) )
494 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
496 if ( !$meta_cache ) {
497 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
498 $meta_cache = $meta_cache[$object_id];
505 if ( isset($meta_cache[$meta_key]) ) {
507 return maybe_unserialize( $meta_cache[$meta_key][0] );
509 return array_map('maybe_unserialize', $meta_cache[$meta_key]);
519 * Determine if a meta key is set for a given object
523 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
524 * @param int $object_id ID of the object metadata is for
525 * @param string $meta_key Metadata key.
526 * @return bool True of the key is set, false if not.
528 function metadata_exists( $meta_type, $object_id, $meta_key ) {
529 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
533 $object_id = absint( $object_id );
534 if ( ! $object_id ) {
538 /** This filter is documented in wp-includes/meta.php */
539 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
540 if ( null !== $check )
541 return (bool) $check;
543 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
545 if ( !$meta_cache ) {
546 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
547 $meta_cache = $meta_cache[$object_id];
550 if ( isset( $meta_cache[ $meta_key ] ) )
557 * Get meta data by meta ID
561 * @global wpdb $wpdb WordPress database abstraction object.
563 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
564 * @param int $meta_id ID for a specific meta row
565 * @return object|false Meta object or false.
567 function get_metadata_by_mid( $meta_type, $meta_id ) {
570 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
574 $meta_id = absint( $meta_id );
579 $table = _get_meta_table( $meta_type );
584 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
586 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
588 if ( empty( $meta ) )
591 if ( isset( $meta->meta_value ) )
592 $meta->meta_value = maybe_unserialize( $meta->meta_value );
598 * Update meta data by meta ID
602 * @global wpdb $wpdb WordPress database abstraction object.
604 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
605 * @param int $meta_id ID for a specific meta row
606 * @param string $meta_value Metadata value
607 * @param string $meta_key Optional, you can provide a meta key to update it
608 * @return bool True on successful update, false on failure.
610 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
613 // Make sure everything is valid.
614 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
618 $meta_id = absint( $meta_id );
623 $table = _get_meta_table( $meta_type );
628 $column = sanitize_key($meta_type . '_id');
629 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
631 // Fetch the meta and go on if it's found.
632 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
633 $original_key = $meta->meta_key;
634 $object_id = $meta->{$column};
636 // If a new meta_key (last parameter) was specified, change the meta key,
637 // otherwise use the original key in the update statement.
638 if ( false === $meta_key ) {
639 $meta_key = $original_key;
640 } elseif ( ! is_string( $meta_key ) ) {
645 $_meta_value = $meta_value;
646 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
647 $meta_value = maybe_serialize( $meta_value );
649 // Format the data query arguments.
651 'meta_key' => $meta_key,
652 'meta_value' => $meta_value
655 // Format the where query arguments.
657 $where[$id_column] = $meta_id;
659 /** This action is documented in wp-includes/meta.php */
660 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
662 if ( 'post' == $meta_type ) {
663 /** This action is documented in wp-includes/meta.php */
664 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
667 // Run the update query, all fields in $data are %s, $where is a %d.
668 $result = $wpdb->update( $table, $data, $where, '%s', '%d' );
673 wp_cache_delete($object_id, $meta_type . '_meta');
675 /** This action is documented in wp-includes/meta.php */
676 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
678 if ( 'post' == $meta_type ) {
679 /** This action is documented in wp-includes/meta.php */
680 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
686 // And if the meta was not found.
691 * Delete meta data by meta ID
695 * @global wpdb $wpdb WordPress database abstraction object.
697 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
698 * @param int $meta_id ID for a specific meta row
699 * @return bool True on successful delete, false on failure.
701 function delete_metadata_by_mid( $meta_type, $meta_id ) {
704 // Make sure everything is valid.
705 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
709 $meta_id = absint( $meta_id );
714 $table = _get_meta_table( $meta_type );
719 // object and id columns
720 $column = sanitize_key($meta_type . '_id');
721 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
723 // Fetch the meta and go on if it's found.
724 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
725 $object_id = $meta->{$column};
727 /** This action is documented in wp-includes/meta.php */
728 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
731 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
733 * Fires immediately before deleting post or comment metadata of a specific type.
735 * The dynamic portion of the hook, `$meta_type`, refers to the meta
736 * object type (post or comment).
740 * @param int $meta_id ID of the metadata entry to delete.
742 do_action( "delete_{$meta_type}meta", $meta_id );
745 // Run the query, will return true if deleted, false otherwise
746 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
749 wp_cache_delete($object_id, $meta_type . '_meta');
751 /** This action is documented in wp-includes/meta.php */
752 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
755 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
757 * Fires immediately after deleting post or comment metadata of a specific type.
759 * The dynamic portion of the hook, `$meta_type`, refers to the meta
760 * object type (post or comment).
764 * @param int $meta_ids Deleted metadata entry ID.
766 do_action( "deleted_{$meta_type}meta", $meta_id );
773 // Meta id was not found.
778 * Update the metadata cache for the specified objects.
782 * @global wpdb $wpdb WordPress database abstraction object.
784 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
785 * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
786 * @return array|false Metadata cache for the specified objects, or false on failure.
788 function update_meta_cache($meta_type, $object_ids) {
791 if ( ! $meta_type || ! $object_ids ) {
795 $table = _get_meta_table( $meta_type );
800 $column = sanitize_key($meta_type . '_id');
802 if ( !is_array($object_ids) ) {
803 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
804 $object_ids = explode(',', $object_ids);
807 $object_ids = array_map('intval', $object_ids);
809 $cache_key = $meta_type . '_meta';
812 foreach ( $object_ids as $id ) {
813 $cached_object = wp_cache_get( $id, $cache_key );
814 if ( false === $cached_object )
817 $cache[$id] = $cached_object;
824 $id_list = join( ',', $ids );
825 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
826 $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 );
828 if ( !empty($meta_list) ) {
829 foreach ( $meta_list as $metarow) {
830 $mpid = intval($metarow[$column]);
831 $mkey = $metarow['meta_key'];
832 $mval = $metarow['meta_value'];
834 // Force subkeys to be array type:
835 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
836 $cache[$mpid] = array();
837 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
838 $cache[$mpid][$mkey] = array();
840 // Add a value to the current pid/key:
841 $cache[$mpid][$mkey][] = $mval;
845 foreach ( $ids as $id ) {
846 if ( ! isset($cache[$id]) )
847 $cache[$id] = array();
848 wp_cache_add( $id, $cache[$id], $cache_key );
855 * Retrieves the queue for lazy-loading metadata.
859 * @return WP_Metadata_Lazyloader $lazyloader Metadata lazyloader queue.
861 function wp_metadata_lazyloader() {
862 static $wp_metadata_lazyloader;
864 if ( null === $wp_metadata_lazyloader ) {
865 $wp_metadata_lazyloader = new WP_Metadata_Lazyloader();
868 return $wp_metadata_lazyloader;
872 * Given a meta query, generates SQL clauses to be appended to a main query.
878 * @param array $meta_query A meta query.
879 * @param string $type Type of meta.
880 * @param string $primary_table Primary database table name.
881 * @param string $primary_id_column Primary ID column name.
882 * @param object $context Optional. The main query object
883 * @return array Associative array of `JOIN` and `WHERE` SQL.
885 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
886 $meta_query_obj = new WP_Meta_Query( $meta_query );
887 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
891 * Retrieve the name of the metadata table for the specified object type.
895 * @global wpdb $wpdb WordPress database abstraction object.
897 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
898 * @return string|false Metadata table name, or false if no metadata table exists
900 function _get_meta_table($type) {
903 $table_name = $type . 'meta';
905 if ( empty($wpdb->$table_name) )
908 return $wpdb->$table_name;
912 * Determine whether a meta key is protected.
916 * @param string $meta_key Meta key
917 * @param string|null $meta_type
918 * @return bool True if the key is protected, false otherwise.
920 function is_protected_meta( $meta_key, $meta_type = null ) {
921 $protected = ( '_' == $meta_key[0] );
924 * Filters whether a meta key is protected.
928 * @param bool $protected Whether the key is protected. Default false.
929 * @param string $meta_key Meta key.
930 * @param string $meta_type Meta type.
932 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
936 * Sanitize meta value.
940 * @param string $meta_key Meta key.
941 * @param mixed $meta_value Meta value to sanitize.
942 * @param string $object_type Type of object the meta is registered to.
944 * @return mixed Sanitized $meta_value.
946 function sanitize_meta( $meta_key, $meta_value, $object_type ) {
948 * Filters the sanitization of a specific meta key of a specific meta type.
950 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
951 * refer to the metadata object type (comment, post, or user) and the meta
952 * key value, respectively.
956 * @param mixed $meta_value Meta value to sanitize.
957 * @param string $meta_key Meta key.
958 * @param string $object_type Object type.
960 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
964 * Registers a meta key.
967 * @since 4.6.0 {@link https://make.wordpress.org/core/2016/07/08/enhancing-register_meta-in-4-6/ Modified
968 * to support an array of data to attach to registered meta keys}. Previous arguments for
969 * `$sanitize_callback` and `$auth_callback` have been folded into this array.
971 * @param string $object_type Type of object this meta is registered to.
972 * @param string $meta_key Meta key to register.
973 * @param array $args {
974 * Data used to describe the meta key when registered.
976 * @type string $type The type of data associated with this meta key.
977 * @type string $description A description of the data attached to this meta key.
978 * @type bool $single Whether the meta key has one value per object, or an array of values per object.
979 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
980 * @type string $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
981 * @type bool $show_in_rest Whether data associated with this meta key can be considered public.
983 * @param string|array $deprecated Deprecated. Use `$args` instead.
985 * @return bool True if the meta key was successfully registered in the global array, false if not.
986 * Registering a meta key with distinct sanitize and auth callbacks will fire those
987 * callbacks, but will not add to the global registry.
989 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
990 global $wp_meta_keys;
992 if ( ! is_array( $wp_meta_keys ) ) {
993 $wp_meta_keys = array();
1000 'sanitize_callback' => null,
1001 'auth_callback' => null,
1002 'show_in_rest' => false,
1005 // There used to be individual args for sanitize and auth callbacks
1006 $has_old_sanitize_cb = false;
1007 $has_old_auth_cb = false;
1009 if ( is_callable( $args ) ) {
1011 'sanitize_callback' => $args,
1014 $has_old_sanitize_cb = true;
1016 $args = (array) $args;
1019 if ( is_callable( $deprecated ) ) {
1020 $args['auth_callback'] = $deprecated;
1021 $has_old_auth_cb = true;
1025 * Filters the registration arguments when registering meta.
1029 * @param array $args Array of meta registration arguments.
1030 * @param array $defaults Array of default arguments.
1031 * @param string $object_type Object type.
1032 * @param string $meta_key Meta key.
1034 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
1035 $args = wp_parse_args( $args, $defaults );
1037 // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
1038 if ( empty( $args['auth_callback'] ) ) {
1039 if ( is_protected_meta( $meta_key, $object_type ) ) {
1040 $args['auth_callback'] = '__return_false';
1042 $args['auth_callback'] = '__return_true';
1046 // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
1047 if ( is_callable( $args['sanitize_callback'] ) ) {
1048 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
1051 if ( is_callable( $args['auth_callback'] ) ) {
1052 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
1055 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
1056 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
1057 $wp_meta_keys[ $object_type ][ $meta_key ] = $args;
1066 * Checks if a meta key is registered.
1070 * @param string $object_type The type of object.
1071 * @param string $meta_key The meta key.
1073 * @return bool True if the meta key is registered to the object type. False if not.
1075 function registered_meta_key_exists( $object_type, $meta_key ) {
1076 global $wp_meta_keys;
1078 if ( ! is_array( $wp_meta_keys ) ) {
1082 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
1086 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {
1094 * Unregisters a meta key from the list of registered keys.
1098 * @param string $object_type The type of object.
1099 * @param string $meta_key The meta key.
1100 * @return bool True if successful. False if the meta key was not registered.
1102 function unregister_meta_key( $object_type, $meta_key ) {
1103 global $wp_meta_keys;
1105 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
1109 $args = $wp_meta_keys[ $object_type ][ $meta_key ];
1111 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
1112 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
1115 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
1116 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
1119 unset( $wp_meta_keys[ $object_type ][ $meta_key ] );
1122 if ( empty( $wp_meta_keys[ $object_type ] ) ) {
1123 unset( $wp_meta_keys[ $object_type ] );
1130 * Retrieves a list of registered meta keys for an object type.
1134 * @param string $object_type The type of object. Post, comment, user, term.
1135 * @return array List of registered meta keys.
1137 function get_registered_meta_keys( $object_type ) {
1138 global $wp_meta_keys;
1140 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) {
1144 return $wp_meta_keys[ $object_type ];
1148 * Retrieves registered metadata for a specified object.
1152 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
1153 * @param int $object_id ID of the object the metadata is for.
1154 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
1155 * metadata for the specified object.
1156 * @return mixed A single value or array of values for a key if specified. An array of all registered keys
1157 * and values for an object ID if not.
1159 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
1160 if ( ! empty( $meta_key ) ) {
1161 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
1164 $meta_keys = get_registered_meta_keys( $object_type );
1165 $meta_key_data = $meta_keys[ $meta_key ];
1167 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
1172 $data = get_metadata( $object_type, $object_id );
1174 $meta_keys = get_registered_meta_keys( $object_type );
1175 $registered_data = array();
1177 // Someday, array_filter()
1178 foreach ( $meta_keys as $k => $v ) {
1179 if ( isset( $data[ $k ] ) ) {
1180 $registered_data[ $k ] = $data[ $k ];
1184 return $registered_data;
1188 * Filter out `register_meta()` args based on a whitelist.
1189 * `register_meta()` args may change over time, so requiring the whitelist
1190 * to be explicitly turned off is a warranty seal of sorts.
1195 * @param array $args Arguments from `register_meta()`.
1196 * @param array $default_args Default arguments for `register_meta()`.
1198 * @return array Filtered arguments.
1200 function _wp_register_meta_args_whitelist( $args, $default_args ) {
1201 $whitelist = array_keys( $default_args );
1203 // In an anonymous function world, this would be better as an array_filter()
1204 foreach ( $args as $key => $value ) {
1205 if ( ! in_array( $key, $whitelist ) ) {
1206 unset( $args[ $key ] );