Using instance_id to sort shortcodes

Since Easy Property Listings 3.3 shortcodes support an option that is used for unique pagination when using multiple shortcodes on the same page called instance_id.

[listing instance_id="sort_sold"]

This option can also be used to apply custom sorting to specific shortcodes.

Sort sold listings by date sold

Use the following code snippet and instnace_id to sort your sold listings by date sold.

Shortcode:

[listing status=sold instance_id="sort_sold"]

Code snippet:

/**
 * Sort a specific epl shortcode by instance_id
 *
 * E.g: [listing status=sold instance_id="sort_sold"]
 *
 * @requires EPL 3.3+
 */
function my_epl_instance_id_sort_sold_callback( $query ) {

	// Do nothing if using sorting options.
	if ( isset ( $_GET['sortby'] ) ) {
		return;
	}

	if ( $query->get( 'is_epl_shortcode' ) && 'sort_sold' === $query->get( 'instance_id' ) ) {

		$meta_query = $query->get( 'meta_query' );
		$meta_query[] = array(
			'key'		=> 'property_sold_date',
			'type'		=> 'DATE',
			'compare'	=> 'EXISTS',
		);

		$query->set( 'meta_query', $meta_query );
		$query->set( 'meta_key', 'property_sold_date' );
		$query->set( 'orderby', array( 'meta_value' => 'DESC' ) );
	}
}
add_action( 'pre_get_posts', 'my_epl_instance_id_sort_sold_callback' , 20  );

Sort by current first

Use the following code snippet and instnace_id to sort your listings by current first.

Shortcode:

[listing instance_id="sort_current_first"]

Code snippet:

/**
 * Sort a specific epl shortcode by instance_id
 *
 * E.g: [listing instance_id="sort_current_first"]
 *
 * @requires EPL 3.3+
 */
function my_epl_instance_id_sort_current_first_callback( $query ) {

	// Do nothing if using sorting options.
	if ( isset ( $_GET['sortby'] ) ) {
		return;
	}

	if ( $query->get( 'is_epl_shortcode' ) && 'sort_current_first' === $query->get('instance_id') ) {
		$query->set( 'meta_key', 'property_status' );
		$query->set( 'orderby', array( 'meta_value' => 'ASC' ) );
	}

}
add_action( 'pre_get_posts', 'my_epl_instance_id_sort_current_first_callback' , 20  );