Post_Repository::handle_order_by( string $order_by )

Handles the order_by clauses for events


Parameters #

$order_by

(string) (Required) The key used to order events; e.g. event_date to order events by start date.


Top ↑

Source #

File: src/Tribe/Repositories/Post_Repository.php

	public function handle_order_by( $order_by ) {
		$check_orderby = $order_by;

		if ( ! is_array( $check_orderby ) ) {
			$check_orderby = explode( ' ', $check_orderby );
		}

		$timestamp_key = 'TIMESTAMP(mt1.meta_value)';

		$after = false;
		$loop  = 0;

		foreach ( $check_orderby as $key => $value ) {
			$loop ++;
			$order_by      = is_numeric( $key ) ? $value : $key;
			$order         = is_numeric( $key ) ? 'ASC' : $value;
			$default_order = Arr::get_in_any( [ $this->query_args, $this->default_args ], 'order', 'ASC' );

			switch ( $order_by ) {
				case 'event_date':
					$this->order_by_date( false, $after );
					break;
				case 'event_date_utc':
					$this->order_by_date( true, $after );
					break;
				default:
					$after = $after || 1 === $loop;
					if ( empty( $this->query_args['orderby'] ) ) {
						$this->query_args['orderby'] = [ $order_by => $order ];
					} else {
						$add = [ $order_by => $order ];
						// Make sure all `orderby` clauses have the shape `<orderby> => <order>`.
						$normalized = [];
						foreach ( $this->query_args['orderby'] as $k => $v ) {
							$the_order_by                = is_numeric( $k ) ? $v : $k;
							$the_order                   = is_numeric( $k ) ? $default_order : $v;
							$normalized[ $the_order_by ] = $the_order;
						}
						$this->query_args['orderby'] = $normalized;
						$this->query_args['orderby'] = array_merge( $this->query_args['orderby'], $add );
					}
					break;
			}
		}
	}

Top ↑

Changelog #

Changelog
Version Description
4.12.1 Introduced.