• LifterLMS
  • Knowledge Base
  • Academy
  • Blog
  • Podcast
  • Contributors
  • My Account

LifterLMS LifterLMS

  • Code Reference
  • REST API
  • LLMS-CLI
  • Changelog Subscribe

Code Reference

Skip to content
Filter by type:
Search
Browse: Home / Code Reference / Classes / LLMS_Query_User_Postmeta / LLMS_Query_User_Postmeta::sql_where()

LLMS_Query_User_Postmeta::sql_where()

SQL “where” clause for the query

Contents

  • Return
  • Source
  • Related
    • Used By
  • Changelog
  • User Contributed Notes

Return #Return

(string)


Top ↑

Source #Source

File: includes/class.llms.query.user.postmeta.php

	 * @since    3.15.0
	 * @version  3.15.0
	 */
	protected function sql_where() {

		global $wpdb;

		$sql = 'WHERE 1';

		foreach ( array( 'post_id', 'user_id' ) as $key ) {

			$ids = $this->get( $key );
			if ( $ids ) {
				$prepared = implode( ',', $ids );
				$sql     .= " AND {$key} IN ({$prepared})";
			}
		}

		if ( $this->get( 'query' ) ) {

			$sql .= ' AND ( ';

			foreach ( $this->get( 'query' ) as $i => $query ) {

				if ( 0 !== $i ) {
					$sql .= " {$this->get( 'query_compare' )} ";
				}

				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

				switch ( $query['compare'] ) {

					case '=':
					case '!=':
					case 'LIKE':
						$sql .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} %s )", $query['key'], $query['value'] );
						break;

					case 'IN':
					case 'NOT IN':
						$query['value'] = array_map( array( $this, 'escape_and_quote_string' ), $query['value'] );
						$vals           = implode( ',', $query['value'] );
						$sql           .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} ( {$vals} ) )", $query['key'] );
						break;

					case 'IS NOT NULL':
						$sql .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} )", $query['key'] );
						break;

				}

				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared

			}

			$sql .= ' )';

		}

Expand full source code Collapse full source code View on GitHub


Top ↑

Related #Related

Used By #Used By

Used By
Used By Description
includes/class.llms.query.user.postmeta.php: LLMS_Query_User_Postmeta::prepare_query()

Prepare the SQL for the query.


Top ↑

Changelog #Changelog

Changelog
Version Description
3.15.0 Introduced.

Top ↑

User Contributed Notes #User Contributed Notes

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

© 2014 - 2025 LifterLMS · Privacy Policy · Terms and Conditions

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.