Skip to content

Instantly share code, notes, and snippets.

@caratage
Created December 9, 2012 07:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save caratage/4243710 to your computer and use it in GitHub Desktop.
Sort admin columns by meta key query
/**********************************************
* Sort columns for TOUR DATE post type
**********************************************/
function set_date_post_types_admin_order($wp_query) {
if (is_admin()) {
// Get the post type from the query
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'date') {
// check if parameter is not set yet
if (!isset($_GET['orderby'])) {
// 'orderby' value can be any column name. Meta values need to be accompanied by their meta_key
$wp_query->set('orderby', 'meta_value');
$wp_query->set('meta_key', '_date_day');
// add a meta_query to narrow down results if needed
$wp_query->set('meta_query', array(
array(
'key' => '_date_day',
'value' => strtotime('00:00:00'), // convert today into unix timestamp
'compare' => '>=' // today or in the future
)
)
);
// 'order' value can be ASC or DESC
$wp_query->set('order', 'ASC');
}
}
}
}
add_filter('pre_get_posts', 'set_date_post_types_admin_order');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment