LLMS_Membership::query_associated_posts( string $post_type, string $enabled_key, string $enabled_value, string $list_key )

Performs a WPDB query to retrieve posts associated with the membership


Description Description

See also See also


Top ↑

Parameters Parameters

$post_type

(string) (Required) Post type to query for an association with.

$enabled_key

(string) (Required) A meta key name, used to check if the association is enabled for the associated post. For example: "_llms_is_restricted"

$enabled_value

(string) (Required) The meta value of the $enabled_key when the association is enabled. For example "yes" when checking "_llms_is_restricted"..

$list_key

(string) (Required) The meta key name where associations are stored as a serialized array of WP_Post IDs. For example "_llms_restricted_levels".


Top ↑

Return Return

(int[])


Top ↑

Source Source

File: includes/models/model.llms.membership.php

	protected function query_associated_posts( $post_type, $enabled_key, $enabled_value, $list_key ) {

		global $wpdb;

		// See if we have a cached result first.
		$cache = sprintf( 'membership_%1$d_associated_%2$s', $this->get( 'id' ), $post_type );
		$found = null;
		$ids   = wp_cache_get( $cache, '', false, $found );

		// We don't, perform a query.
		if ( ! $found ) {

			$ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
				$wpdb->prepare(
					"SELECT metas.post_id
				 FROM {$wpdb->postmeta} AS metas
				 JOIN {$wpdb->postmeta} AS metas2 ON metas2.post_id = metas.post_id
				 JOIN {$wpdb->posts} AS posts ON posts.ID = metas.post_id
				 WHERE 1
				   AND posts.post_status = 'publish'
				   AND posts.post_type = %s
				   AND metas2.meta_key = %s
				   AND metas2.meta_value = %s
				   AND metas.meta_key = %s
				   AND metas.meta_value REGEXP %s;",
					$post_type,
					$enabled_key,
					$enabled_value,
					$list_key,
					'a:[0-9][0-9]*:\{(i:[0-9][0-9]*;(i|s:[0-9][0-9]*):"?[0-9][0-9]*"?;)*(i:[0-9][0-9]*;(i|s:[0-9][0-9]*):"?' . $this->get( 'id' ) . '"?;)'
				)
			);

			// Only return ints.
			$ids = array_map( 'absint', $ids );

			// Cache the result.
			wp_cache_set( $cache, $ids );

		}

		return $ids;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 Escape { character in SQL query to add MySQL 8.0 support.
3.38.1 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.