Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Last active August 29, 2015 14:07
Show Gist options
  • Save jessepearson/9698a94769efa097c9ac to your computer and use it in GitHub Desktop.
Save jessepearson/9698a94769efa097c9ac to your computer and use it in GitHub Desktop.
<?php
// func to sort the columns
add_action( 'pre_get_posts', 'floorplan_order_columns', 9999 );
function floorplan_order_columns( $query ) {
// if we're not in the admin, or if it isn't the custom post type, exit
if( ! is_admin() || $query->get( 'post_type' ) !== 'floorplan' )
return;
// get the orderby, if it is not set, leave it blank
$orderby = ( ! empty( $query->query[ 'orderby' ] ) ) ? $query->query[ 'orderby' ] : '';
// get the order and set it, we want our default to asc and not desc
$order = ( ! empty( $query->query[ 'order' ] ) ) ? $query->query[ 'order' ] : 'asc';
$query->set( 'order', $order );
// set our orderby for our columns
switch( $orderby ) {
case 'fp_beds':
$query->set( 'meta_key', 'fp_beds' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_baths':
$query->set( 'meta_key', 'fp_baths' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_den':
$query->set( 'meta_key', 'fp_den' );
$query->set( 'orderby', 'meta_value' );
break;
case 'fp_park':
$query->set( 'meta_key', 'fp_park' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_sf_in':
$query->set( 'meta_key', 'fp_sf_in' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_sf_gar':
$query->set( 'meta_key', 'fp_sf_gar' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_sf_tot':
$query->set( 'meta_key', 'fp_sf_tot' );
$query->set( 'orderby', 'meta_value_num' );
break;
case 'fp_pdf':
$query->set( 'meta_key', 'fp_pdf' );
$query->set( 'orderby', 'meta_value' );
break;
case 'title':
$query->set( 'orderby', 'title' );
break;
default:
$query->set( 'orderby', 'menu_order' );
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment