BP_Media_Model::where( $columns, bool $offset = false, bool $per_page = false, string $order_by = 'id desc' )

Fetch data from table with conditions on column

Description

Parameters

$columns

(Required)

$offset

(Optional)

Default value: false

$per_page

(Optional)

Default value: false

$order_by

(Optional)

Default value: 'id desc'

Return

(array|null|object)

Source

File: bp-media/classes/class-bp-media-model.php

	static function where( $columns, $offset = false, $per_page = false, $order_by = 'id desc' ) {
		$select = 'SELECT * FROM ' . self::_table();
		$where  = ' where 2=2 ';
		foreach ( $columns as $colname => $colvalue ) {
			if ( is_array( $colvalue ) ) {
				if ( ! isset( $colvalue['compare'] ) ) {
					$compare = 'IN';
				} else {
					$compare = $colvalue['compare'];
				}
				if ( ! isset( $colvalue['value'] ) ) {
					$colvalue['value'] = $colvalue;
				}
				$col_val_compare = ( $colvalue['value'] ) ? '(\'' . implode( "','", $colvalue['value'] ) . '\')' : '';
				$where           .= " AND " . self::_table() . ".{$colname} {$compare} {$col_val_compare}";
			} else {
				$where .= " AND " . self::_table() . ".{$colname} = '{$colvalue}'";
			}
		}
		$sql = $select . $where;
		$sql .= " ORDER BY " . self::_table() . ".$order_by";
		if ( false !== $offset ) {
			if ( ! is_integer( $offset ) ) {
				$offset = 0;
			}
			if ( intval( $offset ) < 0 ) {
				$offset = 0;
			}
			if ( ! is_integer( $per_page ) ) {
				$per_page = 1;
			}
			if ( intval( $per_page ) < 0 ) {
				$per_page = 1;
			}
			$sql .= ' LIMIT ' . $offset . ',' . $per_page;
		}
		global $wpdb;

		return $wpdb->get_results( $sql );
	}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.