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.
18 * @uses $wpdb WordPress database object for queries.
19 * @uses do_action() Calls 'added_{$meta_type}_meta' with meta_id of added metadata entry,
20 * object ID, meta key, and meta value
22 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
23 * @param int $object_id ID of the object metadata is for
24 * @param string $meta_key Metadata key
25 * @param string $meta_value Metadata value
26 * @param bool $unique Optional, default is false. Whether the specified metadata key should be
27 * unique for the object. If true, and the object already has a value for the specified
28 * metadata key, no change will be made
29 * @return bool The meta ID on successful update, false on failure.
31 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
32 if ( !$meta_type || !$meta_key )
35 if ( !$object_id = absint($object_id) )
38 if ( ! $table = _get_meta_table($meta_type) )
43 $column = esc_sql($meta_type . '_id');
45 // expected_slashed ($meta_key)
46 $meta_key = stripslashes($meta_key);
47 $meta_value = stripslashes_deep($meta_value);
48 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
50 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
51 if ( null !== $check )
54 if ( $unique && $wpdb->get_var( $wpdb->prepare(
55 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
56 $meta_key, $object_id ) ) )
59 $_meta_value = $meta_value;
60 $meta_value = maybe_serialize( $meta_value );
62 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
64 $result = $wpdb->insert( $table, array(
65 $column => $object_id,
66 'meta_key' => $meta_key,
67 'meta_value' => $meta_value
73 $mid = (int) $wpdb->insert_id;
75 wp_cache_delete($object_id, $meta_type . '_meta');
77 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
83 * Update metadata for the specified object. If no value already exists for the specified object
84 * ID and metadata key, the metadata will be added.
87 * @uses $wpdb WordPress database object for queries.
88 * @uses do_action() Calls 'update_{$meta_type}_meta' before updating metadata with meta_id of
89 * metadata entry to update, object ID, meta key, and meta value
90 * @uses do_action() Calls 'updated_{$meta_type}_meta' after updating metadata with meta_id of
91 * updated metadata entry, object ID, meta key, and meta value
93 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
94 * @param int $object_id ID of the object metadata is for
95 * @param string $meta_key Metadata key
96 * @param string $meta_value Metadata value
97 * @param string $prev_value Optional. If specified, only update existing metadata entries with
98 * the specified value. Otherwise, update all entries.
99 * @return bool True on successful update, false on failure.
101 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
102 if ( !$meta_type || !$meta_key )
105 if ( !$object_id = absint($object_id) )
108 if ( ! $table = _get_meta_table($meta_type) )
113 $column = esc_sql($meta_type . '_id');
114 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
116 // expected_slashed ($meta_key)
117 $meta_key = stripslashes($meta_key);
118 $passed_value = $meta_value;
119 $meta_value = stripslashes_deep($meta_value);
120 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
122 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
123 if ( null !== $check )
124 return (bool) $check;
126 // Compare existing value to new value if no prev value given and the key exists only once.
127 if ( empty($prev_value) ) {
128 $old_value = get_metadata($meta_type, $object_id, $meta_key);
129 if ( count($old_value) == 1 ) {
130 if ( $old_value[0] === $meta_value )
135 if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
136 return add_metadata($meta_type, $object_id, $meta_key, $passed_value);
138 $_meta_value = $meta_value;
139 $meta_value = maybe_serialize( $meta_value );
141 $data = compact( 'meta_value' );
142 $where = array( $column => $object_id, 'meta_key' => $meta_key );
144 if ( !empty( $prev_value ) ) {
145 $prev_value = maybe_serialize($prev_value);
146 $where['meta_value'] = $prev_value;
149 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
151 if ( 'post' == $meta_type )
152 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
154 $wpdb->update( $table, $data, $where );
156 wp_cache_delete($object_id, $meta_type . '_meta');
158 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
160 if ( 'post' == $meta_type )
161 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
167 * Delete metadata for the specified object.
170 * @uses $wpdb WordPress database object for queries.
171 * @uses do_action() Calls 'deleted_{$meta_type}_meta' after deleting with meta_id of
172 * deleted metadata entries, object ID, meta key, and meta value
174 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
175 * @param int $object_id ID of the object metadata is for
176 * @param string $meta_key Metadata key
177 * @param string $meta_value Optional. Metadata value. If specified, only delete metadata entries
178 * with this value. Otherwise, delete all entries with the specified meta_key.
179 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
180 * for all objects, ignoring the specified object_id. Otherwise, only delete matching
181 * metadata entries for the specified object_id.
182 * @return bool True on successful delete, false on failure.
184 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
185 if ( !$meta_type || !$meta_key )
188 if ( (!$object_id = absint($object_id)) && !$delete_all )
191 if ( ! $table = _get_meta_table($meta_type) )
196 $type_column = esc_sql($meta_type . '_id');
197 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
198 // expected_slashed ($meta_key)
199 $meta_key = stripslashes($meta_key);
200 $meta_value = stripslashes_deep($meta_value);
202 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
203 if ( null !== $check )
204 return (bool) $check;
206 $_meta_value = $meta_value;
207 $meta_value = maybe_serialize( $meta_value );
209 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
212 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
215 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
217 $meta_ids = $wpdb->get_col( $query );
218 if ( !count( $meta_ids ) )
222 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
224 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
227 if ( 'post' == $meta_type )
228 do_action( 'delete_postmeta', $meta_ids );
230 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
232 $count = $wpdb->query($query);
238 foreach ( (array) $object_ids as $o_id ) {
239 wp_cache_delete($o_id, $meta_type . '_meta');
242 wp_cache_delete($object_id, $meta_type . '_meta');
245 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
248 if ( 'post' == $meta_type )
249 do_action( 'deleted_postmeta', $meta_ids );
255 * Retrieve metadata for the specified object.
259 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
260 * @param int $object_id ID of the object metadata is for
261 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
262 * the specified object.
263 * @param bool $single Optional, default is false. If true, return only the first value of the
264 * specified meta_key. This parameter has no effect if meta_key is not specified.
265 * @return string|array Single metadata value, or array of values
267 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
271 if ( !$object_id = absint($object_id) )
274 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
275 if ( null !== $check ) {
276 if ( $single && is_array( $check ) )
282 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
284 if ( !$meta_cache ) {
285 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
286 $meta_cache = $meta_cache[$object_id];
292 if ( isset($meta_cache[$meta_key]) ) {
294 return maybe_unserialize( $meta_cache[$meta_key][0] );
296 return array_map('maybe_unserialize', $meta_cache[$meta_key]);
306 * Determine if a meta key is set for a given object
310 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
311 * @param int $object_id ID of the object metadata is for
312 * @param string $meta_key Metadata key.
313 * @return boolean true of the key is set, false if not.
315 function metadata_exists( $meta_type, $object_id, $meta_key ) {
319 if ( ! $object_id = absint( $object_id ) )
322 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
323 if ( null !== $check )
326 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
328 if ( !$meta_cache ) {
329 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
330 $meta_cache = $meta_cache[$object_id];
333 if ( isset( $meta_cache[ $meta_key ] ) )
340 * Get meta data by meta ID
344 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
345 * @param int $meta_id ID for a specific meta row
346 * @return object Meta object or false.
348 function get_metadata_by_mid( $meta_type, $meta_id ) {
354 if ( !$meta_id = absint( $meta_id ) )
357 if ( ! $table = _get_meta_table($meta_type) )
360 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
362 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
364 if ( empty( $meta ) )
367 if ( isset( $meta->meta_value ) )
368 $meta->meta_value = maybe_unserialize( $meta->meta_value );
374 * Update meta data by meta ID
378 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value
379 * and object_id of the given meta_id.
381 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
382 * @param int $meta_id ID for a specific meta row
383 * @param string $meta_value Metadata value
384 * @param string $meta_key Optional, you can provide a meta key to update it
385 * @return bool True on successful update, false on failure.
387 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
390 // Make sure everything is valid.
394 if ( ! $meta_id = absint( $meta_id ) )
397 if ( ! $table = _get_meta_table( $meta_type ) )
400 $column = esc_sql($meta_type . '_id');
401 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
403 // Fetch the meta and go on if it's found.
404 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
405 $original_key = $meta->meta_key;
406 $original_value = $meta->meta_value;
407 $object_id = $meta->{$column};
409 // If a new meta_key (last parameter) was specified, change the meta key,
410 // otherwise use the original key in the update statement.
411 if ( false === $meta_key ) {
412 $meta_key = $original_key;
413 } elseif ( ! is_string( $meta_key ) ) {
418 $_meta_value = $meta_value;
419 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
420 $meta_value = maybe_serialize( $meta_value );
422 // Format the data query arguments.
424 'meta_key' => $meta_key,
425 'meta_value' => $meta_value
428 // Format the where query arguments.
430 $where[$id_column] = $meta_id;
432 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
434 if ( 'post' == $meta_type )
435 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
437 // Run the update query, all fields in $data are %s, $where is a %d.
438 $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' );
441 wp_cache_delete($object_id, $meta_type . '_meta');
443 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
445 if ( 'post' == $meta_type )
446 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
451 // And if the meta was not found.
456 * Delete meta data by meta ID
460 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value
461 * and object_id of the given meta_id.
463 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
464 * @param int $meta_id ID for a specific meta row
465 * @return bool True on successful delete, false on failure.
467 function delete_metadata_by_mid( $meta_type, $meta_id ) {
470 // Make sure everything is valid.
474 if ( ! $meta_id = absint( $meta_id ) )
477 if ( ! $table = _get_meta_table( $meta_type ) )
480 // object and id columns
481 $column = esc_sql($meta_type . '_id');
482 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
484 // Fetch the meta and go on if it's found.
485 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
486 $object_id = $meta->{$column};
488 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
491 if ( 'post' == $meta_type || 'comment' == $meta_type )
492 do_action( "delete_{$meta_type}meta", $meta_id );
494 // Run the query, will return true if deleted, false otherwise
495 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
498 wp_cache_delete($object_id, $meta_type . '_meta');
500 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
503 if ( 'post' == $meta_type || 'comment' == $meta_type )
504 do_action( "deleted_{$meta_type}meta", $meta_id );
510 // Meta id was not found.
515 * Update the metadata cache for the specified objects.
518 * @uses $wpdb WordPress database object for queries.
520 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
521 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for
522 * @return mixed Metadata cache for the specified objects, or false on failure.
524 function update_meta_cache($meta_type, $object_ids) {
525 if ( empty( $meta_type ) || empty( $object_ids ) )
528 if ( ! $table = _get_meta_table($meta_type) )
531 $column = esc_sql($meta_type . '_id');
535 if ( !is_array($object_ids) ) {
536 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids);
537 $object_ids = explode(',', $object_ids);
540 $object_ids = array_map('intval', $object_ids);
542 $cache_key = $meta_type . '_meta';
545 foreach ( $object_ids as $id ) {
546 $cached_object = wp_cache_get( $id, $cache_key );
547 if ( false === $cached_object )
550 $cache[$id] = $cached_object;
557 $id_list = join(',', $ids);
558 $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
559 $meta_type), ARRAY_A );
561 if ( !empty($meta_list) ) {
562 foreach ( $meta_list as $metarow) {
563 $mpid = intval($metarow[$column]);
564 $mkey = $metarow['meta_key'];
565 $mval = $metarow['meta_value'];
567 // Force subkeys to be array type:
568 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) )
569 $cache[$mpid] = array();
570 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) )
571 $cache[$mpid][$mkey] = array();
573 // Add a value to the current pid/key:
574 $cache[$mpid][$mkey][] = $mval;
578 foreach ( $ids as $id ) {
579 if ( ! isset($cache[$id]) )
580 $cache[$id] = array();
581 wp_cache_add( $id, $cache[$id], $cache_key );
588 * Given a meta query, generates SQL clauses to be appended to a main query
594 * @param array $meta_query A meta query
595 * @param string $type Type of meta
596 * @param string $primary_table
597 * @param string $primary_id_column
598 * @param object $context (optional) The main query object
599 * @return array( 'join' => $join_sql, 'where' => $where_sql )
601 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
602 $meta_query_obj = new WP_Meta_Query( $meta_query );
603 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
607 * Container class for a multiple metadata query
611 class WP_Meta_Query {
613 * List of metadata queries. A single query is an associative array:
614 * - 'key' string The meta key
615 * - 'value' string|array The meta value
616 * - 'compare' (optional) string How to compare the key to the value.
617 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
619 * - 'type' string (optional) The type of the value.
620 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
627 public $queries = array();
630 * The relation between the queries. Can be one of 'AND' or 'OR'.
641 * @param array $meta_query (optional) A meta query
643 function __construct( $meta_query = false ) {
647 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
648 $this->relation = 'OR';
650 $this->relation = 'AND';
653 $this->queries = array();
655 foreach ( $meta_query as $key => $query ) {
656 if ( ! is_array( $query ) )
659 $this->queries[] = $query;
664 * Constructs a meta query based on 'meta_*' query vars
669 * @param array $qv The query variables
671 function parse_query_vars( $qv ) {
672 $meta_query = array();
674 // Simple query needs to be first for orderby=meta_value to work correctly
675 foreach ( array( 'key', 'compare', 'type' ) as $key ) {
676 if ( !empty( $qv[ "meta_$key" ] ) )
677 $meta_query[0][ $key ] = $qv[ "meta_$key" ];
680 // WP_Query sets 'meta_value' = '' by default
681 if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] )
682 $meta_query[0]['value'] = $qv[ 'meta_value' ];
684 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
685 $meta_query = array_merge( $meta_query, $qv['meta_query'] );
688 $this->__construct( $meta_query );
692 * Generates SQL clauses to be appended to a main query.
697 * @param string $type Type of meta
698 * @param string $primary_table
699 * @param string $primary_id_column
700 * @param object $context (optional) The main query object
701 * @return array( 'join' => $join_sql, 'where' => $where_sql )
703 function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
706 if ( ! $meta_table = _get_meta_table( $type ) )
709 $meta_id_column = esc_sql( $type . '_id' );
714 $key_only_queries = array();
717 // Split out the meta_key only queries (we can only do this for OR)
718 if ( 'OR' == $this->relation ) {
719 foreach ( $this->queries as $k => $q ) {
720 if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
721 $key_only_queries[$k] = $q;
726 $queries = $this->queries;
729 // Specify all the meta_key only queries in one go
730 if ( $key_only_queries ) {
731 $join[] = "INNER JOIN $meta_table ON $primary_table.$primary_id_column = $meta_table.$meta_id_column";
733 foreach ( $key_only_queries as $key => $q )
734 $where["key-only-$key"] = $wpdb->prepare( "$meta_table.meta_key = %s", trim( $q['key'] ) );
737 foreach ( $queries as $k => $q ) {
738 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
739 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
741 if ( 'NUMERIC' == $meta_type )
742 $meta_type = 'SIGNED';
743 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
746 $meta_value = isset( $q['value'] ) ? $q['value'] : null;
748 if ( isset( $q['compare'] ) )
749 $meta_compare = strtoupper( $q['compare'] );
751 $meta_compare = is_array( $meta_value ) ? 'IN' : '=';
753 if ( ! in_array( $meta_compare, array(
754 '=', '!=', '>', '>=', '<', '<=',
757 'BETWEEN', 'NOT BETWEEN',
763 $alias = $i ? 'mt' . $i : $meta_table;
765 if ( 'NOT EXISTS' == $meta_compare ) {
766 $join[$i] = "LEFT JOIN $meta_table";
767 $join[$i] .= $i ? " AS $alias" : '';
768 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')";
770 $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL';
775 $join[$i] = "INNER JOIN $meta_table";
776 $join[$i] .= $i ? " AS $alias" : '';
777 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
780 if ( !empty( $meta_key ) )
781 $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key );
783 if ( is_null( $meta_value ) ) {
784 if ( empty( $where[$k] ) )
789 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
790 if ( ! is_array( $meta_value ) )
791 $meta_value = preg_split( '/[,\s]+/', $meta_value );
793 if ( empty( $meta_value ) ) {
798 $meta_value = trim( $meta_value );
801 if ( 'IN' == substr( $meta_compare, -2) ) {
802 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
803 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
804 $meta_value = array_slice( $meta_value, 0, 2 );
805 $meta_compare_string = '%s AND %s';
806 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
807 $meta_value = '%' . like_escape( $meta_value ) . '%';
808 $meta_compare_string = '%s';
810 $meta_compare_string = '%s';
813 if ( ! empty( $where[$k] ) )
814 $where[$k] .= ' AND ';
816 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value );
819 $where = array_filter( $where );
821 if ( empty( $where ) )
824 $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )';
826 $join = implode( "\n", $join );
827 if ( ! empty( $join ) )
830 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) );
835 * Retrieve the name of the metadata table for the specified object type.
838 * @uses $wpdb WordPress database object for queries.
840 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
841 * @return mixed Metadata table name, or false if no metadata table exists
843 function _get_meta_table($type) {
846 $table_name = $type . 'meta';
848 if ( empty($wpdb->$table_name) )
851 return $wpdb->$table_name;
855 * Determine whether a meta key is protected
859 * @param string $meta_key Meta key
860 * @return bool True if the key is protected, false otherwise.
862 function is_protected_meta( $meta_key, $meta_type = null ) {
863 $protected = ( '_' == $meta_key[0] );
865 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
869 * Sanitize meta value
873 * @param string $meta_key Meta key
874 * @param mixed $meta_value Meta value to sanitize
875 * @param string $meta_type Type of meta
876 * @return mixed Sanitized $meta_value
878 function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
879 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
887 * @param string $meta_type Type of meta
888 * @param string $meta_key Meta key
889 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
890 * @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.
891 * @param array $args Arguments
893 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
894 if ( is_callable( $sanitize_callback ) )
895 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
897 if ( empty( $auth_callback ) ) {
898 if ( is_protected_meta( $meta_key, $meta_type ) )
899 $auth_callback = '__return_false';
901 $auth_callback = '__return_true';
904 if ( is_callable( $auth_callback ) )
905 add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );