WP_Meta_Query::__construct() WordPress Method

The WP_Meta_Query::__construct() method allows you to create a meta query for retrieving post meta data. This is useful for retrieving data from custom fields.

WP_Meta_Query::__construct( array $meta_query = false ) #

Constructor.


Parameters

$meta_query

(array)(Optional)Array of meta query clauses. When first-order clauses or sub-clauses use strings as their array keys, they may be referenced in the 'orderby' parameter of the parent query.

  • 'relation'
    (string) Optional. The MySQL keyword used to join the clauses of the query. Accepts 'AND' or 'OR'. Default 'AND'.
  • '...$0'
    (array) Optional. An array of first-order clause parameters, or another fully-formed meta query.
    • 'key'
      (string|string[]) Meta key or keys to filter by.
    • 'compare_key'
      (string) MySQL operator used for comparing the $key. Accepts:
      • '='
      • '!='
      • 'LIKE'
      • 'NOT LIKE'
      • 'IN'
      • 'NOT IN'
      • 'REGEXP'
      • 'NOT REGEXP'
      • 'RLIKE',
      • 'EXISTS' (alias of '=')
      • 'NOT EXISTS' (alias of '!=') Default is 'IN' when $key is an array, '=' otherwise.
    • 'type_key'
      (string) MySQL data type that the meta_key column will be CAST to for comparisons. Accepts 'BINARY' for case-sensitive regular expression comparisons. Default is ''.
    • 'value'
      (string|string[]) Meta value or values to filter by.
    • 'compare'
      (string) MySQL operator used for comparing the $value. Accepts:
    • '=',
    • '!='
    • '>'
    • '>='
    • '<'
    • '<='
    • 'LIKE'
    • 'NOT LIKE'
    • 'IN'
    • 'NOT IN'
    • 'BETWEEN'
    • 'NOT BETWEEN'
    • 'REGEXP'
    • 'NOT REGEXP'
    • 'RLIKE'
    • 'EXISTS'
    • 'NOT EXISTS' Default is 'IN' when $value is an array, '=' otherwise.
  • 'type'
    (string) MySQL data type that the meta_value column will be CAST to for comparisons. Accepts:
  • 'NUMERIC'
  • 'BINARY'
  • 'CHAR'
  • 'DATE'
  • 'DATETIME'
  • 'DECIMAL'
  • 'SIGNED'
  • 'TIME'
  • 'UNSIGNED' Default is 'CHAR'.

Default value: false


Top ↑

Source

File: wp-includes/class-wp-meta-query.php

	public function __construct( $meta_query = false ) {
		if ( ! $meta_query ) {
			return;
		}

		if ( isset( $meta_query['relation'] ) && 'OR' === strtoupper( $meta_query['relation'] ) ) {
			$this->relation = 'OR';
		} else {
			$this->relation = 'AND';
		}

		$this->queries = $this->sanitize_query( $meta_query );
	}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Increased the number of operators available to $compare_key. Introduced $type_key, which enables the $key to be cast to a new data type for comparisons.
5.1.0Introduced $compare_key clause parameter, which enables LIKE key matches.
4.2.0Introduced support for naming query clauses by associative array keys.
3.2.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.