WP_Meta_Query::get_sql() WordPress Method

The WP_Meta_Query::get_sql() method is used to generate the SQL code for a meta query. It accepts an array of arguments, which are used to construct the SQL query. The SQL code generated by this method can be used to retrieve data from the database.

WP_Meta_Query::get_sql( string $type, string $primary_table, string $primary_id_column, object $context = null ) #

Generates SQL clauses to be appended to a main query.


Parameters

$type

(string)(Required)Type of meta. Possible values include but are not limited to 'post', 'comment', 'blog', 'term', and 'user'.

$primary_table

(string)(Required)Database table where the object being filtered is stored (eg wp_users).

$primary_id_column

(string)(Required)ID column for the filtered object in $primary_table.

$context

(object)(Optional) The main query object that corresponds to the type, for example a WP_Query, WP_User_Query, or WP_Site_Query.

Default value: null


Top ↑

Return

(string[]|false) Array containing JOIN and WHERE SQL clauses to append to the main query, or false if no table exists for the requested meta type.

  • 'join'
    (string) SQL fragment to append to the main JOIN clause.
  • 'where'
    (string) SQL fragment to append to the main WHERE clause.


Top ↑

Source

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

	public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) {
		$meta_table = _get_meta_table( $type );
		if ( ! $meta_table ) {
			return false;
		}

		$this->table_aliases = array();

		$this->meta_table     = $meta_table;
		$this->meta_id_column = sanitize_key( $type . '_id' );

		$this->primary_table     = $primary_table;
		$this->primary_id_column = $primary_id_column;

		$sql = $this->get_sql_clauses();

		/*
		 * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
		 * be LEFT. Otherwise posts with no metadata will be excluded from results.
		 */
		if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
			$sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
		}

		/**
		 * Filters the meta query's generated SQL.
		 *
		 * @since 3.1.0
		 *
		 * @param string[] $sql               Array containing the query's JOIN and WHERE clauses.
		 * @param array    $queries           Array of meta queries.
		 * @param string   $type              Type of meta. Possible values include but are not limited
		 *                                    to 'post', 'comment', 'blog', 'term', and 'user'.
		 * @param string   $primary_table     Primary table.
		 * @param string   $primary_id_column Primary column ID.
		 * @param object   $context           The main query object that corresponds to the type, for
		 *                                    example a `WP_Query`, `WP_User_Query`, or `WP_Site_Query`.
		 */
		return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
	}


Top ↑

Changelog

Changelog
VersionDescription
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.