Tribe__Repository::where_meta_related_by_meta( string|array $meta_keys, string $compare, string $meta_field = null, string|array $meta_values = null, boolean $or_not_exists = false )

Filters the query to only return posts that are related, via a meta key, to posts that satisfy a condition.


Parameters

$meta_keys

(string|array) (Required) One or more meta_keys relating the queried post type(s) to another post type.

$compare

(string) (Required) The SQL comparison operator.

$meta_field

(string) (Optional) One (a column in the postmeta table) that should match the comparison criteria; required if the comparison operator is not EXISTS or NOT EXISTS.

Default value: null

$meta_values

(string|array) (Optional) One or more values the post field(s) should be compared to; required if the comparison operator is not EXISTS or NOT EXISTS.

Default value: null

$or_not_exists

(boolean) (Optional) Whether or not to also include a clause to check if value IS NULL. Example with this as true: value = X OR value IS NULL.

Default value: false


Top ↑

Return

($this)


Top ↑

Source

File: src/Tribe/Repository.php

	public function where_meta_related_by_meta( $meta_keys, $compare, $meta_field = null, $meta_values = null, $or_not_exists = false ) {
		$meta_keys = Tribe__Utils__Array::list_to_array( $meta_keys );

		if ( ! in_array( $compare, array( 'EXISTS', 'NOT EXISTS' ), true ) ) {
			if ( empty( $meta_field ) || empty( $meta_values ) ) {
				throw Tribe__Repository__Usage_Error::because_this_comparison_operator_requires_fields_and_values( $meta_keys, $compare, $this );
			}
		}

		$meta_field = esc_sql( $meta_field );

		/** @var wpdb $wpdb */
		global $wpdb;

		$pm  = $this->sql_slug( 'post_meta_related_post_meta', $compare, $meta_keys );
		$pmm = $this->sql_slug( 'meta_post_meta_related_post_meta', $compare, $meta_keys );

		$this->filter_query->join( "LEFT JOIN {$wpdb->postmeta} {$pm} ON {$pm}.post_id = {$wpdb->posts}.ID" );
		$this->filter_query->join( "
			LEFT JOIN {$wpdb->postmeta} {$pmm}
				ON {$pmm}.post_id = {$pm}.meta_value
					AND {$pmm}.meta_key = '{$meta_field}'
		" );

		$keys_in = $this->prepare_interval( $meta_keys );

		if ( 'EXISTS' === $compare ) {
			$this->filter_query->where( "
				{$pm}.meta_key IN {$keys_in}
				AND {$pmm}.meta_id IS NOT NULL
			" );
		} elseif ( 'NOT EXISTS' === $compare ) {
			$this->filter_query->where( "
				{$pm}.meta_key IN {$keys_in}
				AND {$pmm}.meta_id IS NULL
			" );
		} else {
			if ( in_array( $compare, static::$multi_value_keys, true ) ) {
				$meta_values = $this->prepare_interval( $meta_values );
			} else {
				$meta_values = $this->prepare_value( $meta_values );
			}

			$clause = "{$pmm}.meta_value {$compare} {$meta_values}";

			if ( $or_not_exists ) {
				$clause = "
					(
						{$clause}
						OR {$pmm}.meta_id IS NULL
					)
				";
			}

			$this->filter_query->where( "
				{$pm}.meta_key IN {$keys_in}
				AND {$clause}
			" );
		}

		return $this;
	}

Top ↑

Changelog

Changelog
Version Description
4.10.3 Introduced.