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 * Filter 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 $meta_key = wp_unslash($meta_key);
165 $passed_value = $meta_value;
166 $meta_value = wp_unslash($meta_value);
167 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
170 * Filter whether to update metadata of a specific type.
172 * The dynamic portion of the hook, `$meta_type`, refers to the meta
173 * object type (comment, post, or user). Returning a non-null value
174 * will effectively short-circuit the function.
178 * @param null|bool $check Whether to allow updating metadata for the given type.
179 * @param int $object_id Object ID.
180 * @param string $meta_key Meta key.
181 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
182 * @param mixed $prev_value Optional. If specified, only update existing
183 * metadata entries with the specified value.
184 * Otherwise, update all entries.
186 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
187 if ( null !== $check )
188 return (bool) $check;
190 // Compare existing value to new value if no prev value given and the key exists only once.
191 if ( empty($prev_value) ) {
192 $old_value = get_metadata($meta_type, $object_id, $meta_key);
193 if ( count($old_value) == 1 ) {
194 if ( $old_value[0] === $meta_value )
199 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
200 if ( empty( $meta_ids ) ) {
201 return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
204 $_meta_value = $meta_value;
205 $meta_value = maybe_serialize( $meta_value );
207 $data = compact( 'meta_value' );
208 $where = array( $column => $object_id, 'meta_key' => $meta_key );
210 if ( !empty( $prev_value ) ) {
211 $prev_value = maybe_serialize($prev_value);
212 $where['meta_value'] = $prev_value;
215 foreach ( $meta_ids as $meta_id ) {
217 * Fires immediately before updating metadata of a specific type.
219 * The dynamic portion of the hook, `$meta_type`, refers to the meta
220 * object type (comment, post, or user).
224 * @param int $meta_id ID of the metadata entry to update.
225 * @param int $object_id Object ID.
226 * @param string $meta_key Meta key.
227 * @param mixed $meta_value Meta value.
229 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
232 if ( 'post' == $meta_type ) {
233 foreach ( $meta_ids as $meta_id ) {
235 * Fires immediately before updating a post's metadata.
239 * @param int $meta_id ID of metadata entry to update.
240 * @param int $object_id Object ID.
241 * @param string $meta_key Meta key.
242 * @param mixed $meta_value Meta value.
244 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
248 $result = $wpdb->update( $table, $data, $where );
252 wp_cache_delete($object_id, $meta_type . '_meta');
254 foreach ( $meta_ids as $meta_id ) {
256 * Fires immediately after updating metadata of a specific type.
258 * The dynamic portion of the hook, `$meta_type`, refers to the meta
259 * object type (comment, post, or user).
263 * @param int $meta_id ID of updated metadata entry.
264 * @param int $object_id Object ID.
265 * @param string $meta_key Meta key.
266 * @param mixed $meta_value Meta value.
268 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
271 if ( 'post' == $meta_type ) {
272 foreach ( $meta_ids as $meta_id ) {
274 * Fires immediately after updating a post's metadata.
278 * @param int $meta_id ID of updated metadata entry.
279 * @param int $object_id Object ID.
280 * @param string $meta_key Meta key.
281 * @param mixed $meta_value Meta value.
283 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
291 * Delete metadata for the specified object.
295 * @global wpdb $wpdb WordPress database abstraction object.
297 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
298 * @param int $object_id ID of the object metadata is for
299 * @param string $meta_key Metadata key
300 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
301 * metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
302 * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility,
303 * it is not possible to pass an empty string to delete those entries with an empty string
305 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
306 * ignoring the specified object_id. Otherwise, only delete matching metadata entries for
307 * the specified object_id.
308 * @return bool True on successful delete, false on failure.
310 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
313 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
317 $object_id = absint( $object_id );
318 if ( ! $object_id && ! $delete_all ) {
322 $table = _get_meta_table( $meta_type );
327 $type_column = sanitize_key($meta_type . '_id');
328 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
329 // expected_slashed ($meta_key)
330 $meta_key = wp_unslash($meta_key);
331 $meta_value = wp_unslash($meta_value);
334 * Filter whether to delete metadata of a specific type.
336 * The dynamic portion of the hook, `$meta_type`, refers to the meta
337 * object type (comment, post, or user). Returning a non-null value
338 * will effectively short-circuit the function.
342 * @param null|bool $delete Whether to allow metadata deletion of the given type.
343 * @param int $object_id Object ID.
344 * @param string $meta_key Meta key.
345 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
346 * @param bool $delete_all Whether to delete the matching metadata entries
347 * for all objects, ignoring the specified $object_id.
350 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
351 if ( null !== $check )
352 return (bool) $check;
354 $_meta_value = $meta_value;
355 $meta_value = maybe_serialize( $meta_value );
357 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
360 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
362 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value )
363 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
365 $meta_ids = $wpdb->get_col( $query );
366 if ( !count( $meta_ids ) )
370 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
373 * Fires immediately before deleting metadata of a specific type.
375 * The dynamic portion of the hook, `$meta_type`, refers to the meta
376 * object type (comment, post, or user).
380 * @param array $meta_ids An array of metadata entry IDs to delete.
381 * @param int $object_id Object ID.
382 * @param string $meta_key Meta key.
383 * @param mixed $meta_value Meta value.
385 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
388 if ( 'post' == $meta_type ) {
390 * Fires immediately before deleting metadata for a post.
394 * @param array $meta_ids An array of post metadata entry IDs to delete.
396 do_action( 'delete_postmeta', $meta_ids );
399 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
401 $count = $wpdb->query($query);
407 foreach ( (array) $object_ids as $o_id ) {
408 wp_cache_delete($o_id, $meta_type . '_meta');
411 wp_cache_delete($object_id, $meta_type . '_meta');
415 * Fires immediately after deleting metadata of a specific type.
417 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
418 * object type (comment, post, or user).
422 * @param array $meta_ids An array of deleted metadata entry IDs.
423 * @param int $object_id Object ID.
424 * @param string $meta_key Meta key.
425 * @param mixed $meta_value Meta value.
427 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
430 if ( 'post' == $meta_type ) {
432 * Fires immediately after deleting metadata for a post.
436 * @param array $meta_ids An array of deleted post metadata entry IDs.
438 do_action( 'deleted_postmeta', $meta_ids );
445 * Retrieve metadata for the specified object.
449 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
450 * @param int $object_id ID of the object metadata is for
451 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
452 * the specified object.
453 * @param bool $single Optional, default is false.
454 * If true, return only the first value of the specified meta_key.
455 * This parameter has no effect if meta_key is not specified.
456 * @return mixed Single metadata value, or array of values
458 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
459 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
463 $object_id = absint( $object_id );
464 if ( ! $object_id ) {
469 * Filter whether to retrieve metadata of a specific type.
471 * The dynamic portion of the hook, `$meta_type`, refers to the meta
472 * object type (comment, post, or user). Returning a non-null value
473 * will effectively short-circuit the function.
477 * @param null|array|string $value The value get_metadata() should return - a single metadata value,
478 * or an array of values.
479 * @param int $object_id Object ID.
480 * @param string $meta_key Meta key.
481 * @param bool $single Whether to return only the first value of the specified $meta_key.
483 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
484 if ( null !== $check ) {
485 if ( $single && is_array( $check ) )
491 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
493 if ( !$meta_cache ) {
494 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
495 $meta_cache = $meta_cache[$object_id];
502 if ( isset($meta_cache[$meta_key]) ) {
504 return maybe_unserialize( $meta_cache[$meta_key][0] );
506 return array_map('maybe_unserialize', $meta_cache[$meta_key]);
516 * Determine if a meta key is set for a given object
520 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
521 * @param int $object_id ID of the object metadata is for
522 * @param string $meta_key Metadata key.
523 * @return bool True of the key is set, false if not.
525 function metadata_exists( $meta_type, $object_id, $meta_key ) {
526 if ( ! $meta_type || ! is_numeric( $object_id ) ) {
530 $object_id = absint( $object_id );
531 if ( ! $object_id ) {
535 /** This filter is documented in wp-includes/meta.php */
536 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
537 if ( null !== $check )
538 return (bool) $check;
540 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
542 if ( !$meta_cache ) {
543 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
544 $meta_cache = $meta_cache[$object_id];
547 if ( isset( $meta_cache[ $meta_key ] ) )
554 * Get meta data by meta ID
558 * @global wpdb $wpdb WordPress database abstraction object.
560 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
561 * @param int $meta_id ID for a specific meta row
562 * @return object|false Meta object or false.
564 function get_metadata_by_mid( $meta_type, $meta_id ) {
567 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
571 $meta_id = absint( $meta_id );
576 $table = _get_meta_table( $meta_type );
581 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
583 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
585 if ( empty( $meta ) )
588 if ( isset( $meta->meta_value ) )
589 $meta->meta_value = maybe_unserialize( $meta->meta_value );
595 * Update meta data by meta ID
599 * @global wpdb $wpdb WordPress database abstraction object.
601 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
602 * @param int $meta_id ID for a specific meta row
603 * @param string $meta_value Metadata value
604 * @param string $meta_key Optional, you can provide a meta key to update it
605 * @return bool True on successful update, false on failure.
607 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
610 // Make sure everything is valid.
611 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
615 $meta_id = absint( $meta_id );
620 $table = _get_meta_table( $meta_type );
625 $column = sanitize_key($meta_type . '_id');
626 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
628 // Fetch the meta and go on if it's found.
629 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
630 $original_key = $meta->meta_key;
631 $object_id = $meta->{$column};
633 // If a new meta_key (last parameter) was specified, change the meta key,
634 // otherwise use the original key in the update statement.
635 if ( false === $meta_key ) {
636 $meta_key = $original_key;
637 } elseif ( ! is_string( $meta_key ) ) {
642 $_meta_value = $meta_value;
643 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
644 $meta_value = maybe_serialize( $meta_value );
646 // Format the data query arguments.
648 'meta_key' => $meta_key,
649 'meta_value' => $meta_value
652 // Format the where query arguments.
654 $where[$id_column] = $meta_id;
656 /** This action is documented in wp-includes/meta.php */
657 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
659 if ( 'post' == $meta_type ) {
660 /** This action is documented in wp-includes/meta.php */
661 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
664 // Run the update query, all fields in $data are %s, $where is a %d.
665 $result = $wpdb->update( $table, $data, $where, '%s', '%d' );
670 wp_cache_delete($object_id, $meta_type . '_meta');
672 /** This action is documented in wp-includes/meta.php */
673 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
675 if ( 'post' == $meta_type ) {
676 /** This action is documented in wp-includes/meta.php */
677 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
683 // And if the meta was not found.
688 * Delete meta data by meta ID
692 * @global wpdb $wpdb WordPress database abstraction object.
694 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
695 * @param int $meta_id ID for a specific meta row
696 * @return bool True on successful delete, false on failure.
698 function delete_metadata_by_mid( $meta_type, $meta_id ) {
701 // Make sure everything is valid.
702 if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
706 $meta_id = absint( $meta_id );
711 $table = _get_meta_table( $meta_type );
716 // object and id columns
717 $column = sanitize_key($meta_type . '_id');
718 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
720 // Fetch the meta and go on if it's found.
721 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
722 $object_id = $meta->{$column};
724 /** This action is documented in wp-includes/meta.php */
725 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
728 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
730 * Fires immediately before deleting post or comment metadata of a specific type.
732 * The dynamic portion of the hook, `$meta_type`, refers to the meta
733 * object type (post or comment).
737 * @param int $meta_id ID of the metadata entry to delete.
739 do_action( "delete_{$meta_type}meta", $meta_id );
742 // Run the query, will return true if deleted, false otherwise
743 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
746 wp_cache_delete($object_id, $meta_type . '_meta');
748 /** This action is documented in wp-includes/meta.php */
749 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
752 if ( 'post' == $meta_type || 'comment' == $meta_type ) {
754 * Fires immediately after deleting post or comment metadata of a specific type.
756 * The dynamic portion of the hook, `$meta_type`, refers to the meta
757 * object type (post or comment).
761 * @param int $meta_ids Deleted metadata entry ID.
763 do_action( "deleted_{$meta_type}meta", $meta_id );
770 // Meta id was not found.
775 * Update the metadata cache for the specified objects.
779 * @global wpdb $wpdb WordPress database abstraction object.
781 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
782 * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
783 * @return array|false Metadata cache for the specified objects, or false on failure.
785 function update_meta_cache($meta_type, $object_ids) {
788 if ( ! $meta_type || ! $object_ids ) {
792 $table = _get_meta_table( $meta_type );
797 $column = sanitize_key($meta_type . '_id');
799 if ( !is_array($object_ids) ) {
800 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
801 $object_ids = explode(',', $object_ids);
804 $object_ids = array_map('intval', $object_ids);
806 $cache_key = $meta_type . '_meta';
809 foreach ( $object_ids as $id ) {
810 $cached_object = wp_cache_get( $id, $cache_key );
811 if ( false === $cached_object )
814 $cache[$id] = $cached_object;
821 $id_list = join( ',', $ids );
822 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
823 $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 );
825 if ( !empty($meta_list) ) {
826 foreach ( $meta_list as $metarow) {
827 $mpid = intval($metarow[$column]);
828 $mkey = $metarow['meta_key'];
829 $mval = $metarow['meta_value'];
831 // Force subkeys to be array type:
832 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
833 $cache[$mpid] = array();
834 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
835 $cache[$mpid][$mkey] = array();
837 // Add a value to the current pid/key:
838 $cache[$mpid][$mkey][] = $mval;
842 foreach ( $ids as $id ) {
843 if ( ! isset($cache[$id]) )
844 $cache[$id] = array();
845 wp_cache_add( $id, $cache[$id], $cache_key );
852 * Given a meta query, generates SQL clauses to be appended to a main query.
858 * @param array $meta_query A meta query.
859 * @param string $type Type of meta.
860 * @param string $primary_table Primary database table name.
861 * @param string $primary_id_column Primary ID column name.
862 * @param object $context Optional. The main query object
863 * @return array Associative array of `JOIN` and `WHERE` SQL.
865 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
866 $meta_query_obj = new WP_Meta_Query( $meta_query );
867 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
871 * Retrieve the name of the metadata table for the specified object type.
875 * @global wpdb $wpdb WordPress database abstraction object.
877 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
878 * @return string|false Metadata table name, or false if no metadata table exists
880 function _get_meta_table($type) {
883 $table_name = $type . 'meta';
885 if ( empty($wpdb->$table_name) )
888 return $wpdb->$table_name;
892 * Determine whether a meta key is protected.
896 * @param string $meta_key Meta key
897 * @param string|null $meta_type
898 * @return bool True if the key is protected, false otherwise.
900 function is_protected_meta( $meta_key, $meta_type = null ) {
901 $protected = ( '_' == $meta_key[0] );
904 * Filter whether a meta key is protected.
908 * @param bool $protected Whether the key is protected. Default false.
909 * @param string $meta_key Meta key.
910 * @param string $meta_type Meta type.
912 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
916 * Sanitize meta value.
920 * @param string $meta_key Meta key
921 * @param mixed $meta_value Meta value to sanitize
922 * @param string $meta_type Type of meta
923 * @return mixed Sanitized $meta_value
925 function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
928 * Filter the sanitization of a specific meta key of a specific meta type.
930 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
931 * refer to the metadata object type (comment, post, or user) and the meta
937 * @param mixed $meta_value Meta value to sanitize.
938 * @param string $meta_key Meta key.
939 * @param string $meta_type Meta type.
941 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
949 * @param string $meta_type Type of meta
950 * @param string $meta_key Meta key
951 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
952 * @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.
954 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
955 if ( is_callable( $sanitize_callback ) )
956 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
958 if ( empty( $auth_callback ) ) {
959 if ( is_protected_meta( $meta_key, $meta_type ) )
960 $auth_callback = '__return_false';
962 $auth_callback = '__return_true';
965 if ( is_callable( $auth_callback ) )
966 add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );