How to set the default sort to current first

Using the WordPress  pre_get_posts filter you can alter the default post type archive page sorting to sort by current listings followed by sold/leased.

This is only going to alter the search result pages e.g.

  • /property/
  • /rental/
  • /land/

We find it better to use shortcodes and use the instance_id option to sort shortcodes.

function my_epl_listing_sort_status( $query ) {
	// Do nothing if in dashboard or not an archive page
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}

	// Do nothing if Easy Property Listings is not active
	if ( ! function_exists( 'epl_all_post_types' ) ) {
		return;
	}

	// Do nothing if doing search
	if ( epl_is_search() ) {
		return;
	}

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

	// Sort EPL listings by price on archive page
	if ( is_post_type_archive( epl_all_post_types() == 'true' ) ) {
		$query->set( 'meta_key', 'property_status' );
	    	$query->set( 'orderby', array(
		    		'meta_value' 	=> 'ASC',
		    		'date' 		=> 'DESC'
	    		)
	    	);
		return;
	}
}
add_action( 'pre_get_posts', 'my_epl_listing_sort_status' , 99  );