*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $object_id ID of the object metadata is for
- * @param string $meta_key Metadata key
- * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
- * @param bool $unique Optional, default is false. Whether the specified metadata key should be
- * unique for the object. If true, and the object already has a value for the specified
- * metadata key, no change will be made
- * @return int|bool The meta ID on success, false on failure.
+ * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
+ * @param int $object_id ID of the object metadata is for
+ * @param string $meta_key Metadata key
+ * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param bool $unique Optional, default is false.
+ * Whether the specified metadata key should be unique for the object.
+ * If true, and the object already has a value for the specified metadata key,
+ * no change will be made.
+ * @return int|false The meta ID on success, false on failure.
*/
function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
global $wpdb;
*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $object_id ID of the object metadata is for
- * @param string $meta_key Metadata key
- * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
- * @param mixed $prev_value Optional. If specified, only update existing metadata entries with
- * the specified value. Otherwise, update all entries.
+ * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
+ * @param int $object_id ID of the object metadata is for
+ * @param string $meta_key Metadata key
+ * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param mixed $prev_value Optional. If specified, only update existing metadata entries with
+ * the specified value. Otherwise, update all entries.
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
*/
function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') {
*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $object_id ID of the object metadata is for
- * @param string $meta_key Metadata key
- * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete metadata entries
- * with this value. Otherwise, delete all entries with the specified meta_key.
- * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries
- * for all objects, ignoring the specified object_id. Otherwise, only delete matching
- * metadata entries for the specified object_id.
+ * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
+ * @param int $object_id ID of the object metadata is for
+ * @param string $meta_key Metadata key
+ * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
+ * metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
+ * Pass `null, `false`, or an empty string to skip this check. (For backward compatibility,
+ * it is not possible to pass an empty string to delete those entries with an empty string
+ * for a value.)
+ * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
+ * ignoring the specified object_id. Otherwise, only delete matching metadata entries for
+ * the specified object_id.
* @return bool True on successful delete, false on failure.
*/
function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) {
if ( !$delete_all )
$query .= $wpdb->prepare(" AND $type_column = %d", $object_id );
- if ( $meta_value )
+ if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value )
$query .= $wpdb->prepare(" AND meta_value = %s", $meta_value );
$meta_ids = $wpdb->get_col( $query );
* @since 2.9.0
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $object_id ID of the object metadata is for
- * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
- * the specified object.
- * @param bool $single Optional, default is false. If true, return only the first value of the
- * specified meta_key. This parameter has no effect if meta_key is not specified.
+ * @param int $object_id ID of the object metadata is for
+ * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for
+ * the specified object.
+ * @param bool $single Optional, default is false.
+ * If true, return only the first value of the specified meta_key.
+ * This parameter has no effect if meta_key is not specified.
* @return mixed Single metadata value, or array of values
*/
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
* @since 3.3.0
*
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $object_id ID of the object metadata is for
- * @param string $meta_key Metadata key.
- * @return boolean true of the key is set, false if not.
+ * @param int $object_id ID of the object metadata is for
+ * @param string $meta_key Metadata key.
+ * @return bool True of the key is set, false if not.
*/
function metadata_exists( $meta_type, $object_id, $meta_key ) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
*
* @since 3.3.0
*
+ * @global wpdb $wpdb
+ *
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $meta_id ID for a specific meta row
- * @return object Meta object or false.
+ * @param int $meta_id ID for a specific meta row
+ * @return object|false Meta object or false.
*/
function get_metadata_by_mid( $meta_type, $meta_id ) {
global $wpdb;
*
* @since 3.3.0
*
- * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $meta_id ID for a specific meta row
+ * @global wpdb $wpdb
+ *
+ * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
+ * @param int $meta_id ID for a specific meta row
* @param string $meta_value Metadata value
- * @param string $meta_key Optional, you can provide a meta key to update it
+ * @param string $meta_key Optional, you can provide a meta key to update it
* @return bool True on successful update, false on failure.
*/
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
*
* @since 3.3.0
*
+ * @global wpdb $wpdb
+ *
* @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int $meta_id ID for a specific meta row
+ * @param int $meta_id ID for a specific meta row
* @return bool True on successful delete, false on failure.
*/
function delete_metadata_by_mid( $meta_type, $meta_id ) {
*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
- * @param int|array $object_ids array or comma delimited list of object IDs to update cache for
- * @return mixed Metadata cache for the specified objects, or false on failure.
+ * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
+ * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
+ * @return array|false Metadata cache for the specified objects, or false on failure.
*/
function update_meta_cache($meta_type, $object_ids) {
global $wpdb;
*/
protected $clauses = array();
+ /**
+ * Whether the query contains any OR relations.
+ *
+ * @since 4.3.0
+ * @access protected
+ * @var bool
+ */
+ protected $has_or_relation = false;
+
/**
* Constructor.
*
// Sanitize the 'relation' key provided in the query.
if ( isset( $relation ) && 'OR' === strtoupper( $relation ) ) {
$clean_queries['relation'] = 'OR';
+ $this->has_or_relation = true;
/*
* If there is only a single clause, call the relation 'OR'.
* @param string $primary_table Database table where the object being filtered is stored (eg wp_users).
* @param string $primary_id_column ID column for the filtered object in $primary_table.
* @param object $context Optional. The main query object.
- * @return array {
+ * @return false|array {
* Array containing JOIN and WHERE SQL clauses to append to the main query.
*
* @type string $join SQL fragment to append to the main JOIN clause.
* @since 4.1.0
* @access public
*
+ * @global wpdb $wpdb
+ *
* @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array.
* @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
*/
return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this ) ;
}
+
+ /**
+ * Checks whether the current query has any OR relations.
+ *
+ * In some cases, the presence of an OR relation somewhere in the query will require
+ * the use of a `DISTINCT` or `GROUP BY` keyword in the `SELECT` clause. The current
+ * method can be used in these cases to determine whether such a clause is necessary.
+ *
+ * @since 4.3.0
+ *
+ * @return bool True if the query contains any `OR` relations, otherwise false.
+ */
+ public function has_or_relation() {
+ return $this->has_or_relation;
+ }
}
/**
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
- * @return mixed Metadata table name, or false if no metadata table exists
+ * @return string|false Metadata table name, or false if no metadata table exists
*/
function _get_meta_table($type) {
global $wpdb;
*
* @since 3.1.3
*
- * @param string $meta_key Meta key
- * @param mixed $meta_value Meta value to sanitize
- * @param string $meta_type Type of meta
+ * @param string $meta_key Meta key
+ * @param mixed $meta_value Meta value to sanitize
+ * @param string $meta_type Type of meta
* @return mixed Sanitized $meta_value
*/
function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
*
* @since 3.3.0
*
- * @param string $meta_type Type of meta
- * @param string $meta_key Meta key
+ * @param string $meta_type Type of meta
+ * @param string $meta_key Meta key
* @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
- * @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.
+ * @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.
*/
function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
if ( is_callable( $sanitize_callback ) )