tribe_normalize_orderby( string|array $orderby, string $order = 'ASC' )

Normalizes an orderby string or array to an map of keys and orders.

Note the function and the variables use the "orderby" (no spaces) name to stick with the WordPress query standard.


Parameters #

$orderby

(string|<span class="array">array) (Required) Either an orderby key, a list of orderby keys or a map of orderby clauses.

$order

(string) (Optional) The default order that should be applied to orderby entries that lack one.

Default value: 'ASC'


Top ↑

Return #

(array) The normalized orderby array, in the format supported by WordPress queries: [ <key_1> => <order>, <key_2> => <order>, ... ].


Top ↑

Source #

File: src/functions/query.php

	function tribe_normalize_orderby( $orderby, $order = 'ASC' ) {
		// Make the `orderby` part an array.
		$orderby_arr = (array) $orderby;
		$normalized  = [];

		foreach ( $orderby_arr as $by_key => $direction ) {
			if ( empty( $direction ) ) {
				continue;
			}

			if ( is_numeric( $by_key ) ) {
				// It's an entry where the key is just listed, relying on the default order.
				$by_key    = $direction;
				$direction = $order;
			}

			$normalized[ $by_key ] = $direction;
		}

		return $normalized;
	}

Top ↑

Changelog #

Changelog
Version Description
4.12.6 Introduced.