epl_sorter_orderby: change the tiebreakers when sorting listings

Description #

When a visitor sorts listings (Price: Low to High, Suburb A-Z, Status and so on), EPL 3.6 adds tiebreakers so listings with the same value always come back in the same order. Without them a listing could show on two pages, or on none, when paging through sorted results. This filter changes the sort order list: the primary sort first, then the tiebreakers.


Usage #

add_filter( 'epl_sorter_orderby', 'my_function', 10, 2 );

Parameters #

$orderby (array)
Orderby => order pairs for WP_Query, primary sort first. For a meta sort the primary key is the sort option's orderby (meta_value or meta_value_num). Default tiebreakers:

  • Date sorts (date, post_date): ID in the same direction.
  • ID sort: none.
  • Every other sort (price, status, suburb, title): 'date' => 'DESC' then 'ID' => 'DESC', newest first.

$sorter (array)
The sort option the visitor picked, from epl_sorting_options(): id, label, type (meta or post), key, order and, for meta sorts, orderby.

The filter isn't called for random sorts (rand, RAND(seed)), none, or a sort option whose orderby already lists several keys: those are used as they are.


Where it runs #

  • Listing archives, location, feature and business category pages, and search results, when sortby is in the address.
  • Listing shortcodes sorted with instance_id. See Using instance_id to sort shortcodes.

Examples #

Break ties in the Suburb A-Z sort by listing title instead of newest first:

function my_epl_sorter_orderby( $orderby, $sorter ) {
	if ( 'location_asc' === $sorter['id'] ) {
		return array(
			'meta_value' => 'ASC',
			'title'      => 'ASC',
			'ID'         => 'ASC',
		);
	}
	return $orderby;
}
add_filter( 'epl_sorter_orderby', 'my_epl_sorter_orderby', 10, 2 );

Keep ID last so every listing has a fixed place. To add your own sort options, see Adding a custom field and creating sorting options. To build the same arguments for your own query, see epl_get_sorter_query_args().


Source #

lib/includes/template-functions.php, in epl_get_sorter_query_args(), called by epl_archive_sorting() and epl_add_orderby_args(). Since 3.6.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.