fbpx
/*
function change_view_order()

applied whenever a view is called.

Allows ANY view to be sorted based on query string ?sort={field ID here}&order={ASC|DESC|RAND} here

*/
add_filter('frm_view_order', 'change_view_order', 10, 2);
function change_view_order($query, $args){
	if( isset($_REQUEST['sort']) && is_numeric($_REQUEST['sort']) ) {
		if(isset($_REQUEST['order']) ) {
			$order = $_REQUEST['order'];
		} else {
			$order = "ASC";
		}
		$query['select'] .= ' LEFT JOIN wp_frm_item_metas em1 ON em1.item_id=it.id AND em1.field_id='.$_REQUEST['sort'];
		$query['order'] = 'ORDER BY em1.meta_value '.$_REQUEST['order'];
		
	}
	return $query;
}