Skip to content

Instantly share code, notes, and snippets.

@attilaacs
Last active October 9, 2019 04:28
Show Gist options
  • Save attilaacs/190293e2c6bab5b39d4f243ce4edaaa2 to your computer and use it in GitHub Desktop.
Save attilaacs/190293e2c6bab5b39d4f243ce4edaaa2 to your computer and use it in GitHub Desktop.
Wordpress Admin: sort custom post type column by ACF date time field
add_action( 'pre_get_posts', 'aa_sortbydatetime');
function aa_sortbydatetime( $query ) {
global $pagenow;
if ( !is_admin() || $pagenow !== 'edit.php' ) {
return;
}
$post_type = $query->get( 'post_type' );
$order_by = $query->get( 'orderby' );
if ( $post_type == 'studio-event' ) {
if ( empty( $order_by ) ) {
$order_by = 'mc_start_date';
}
switch ( $order_by ) {
case 'mc_start_date':
$query->set( 'meta_key', 'mc_start_date' );
$query->set( 'orderby', 'meta_value' );
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment