Last active
October 9, 2019 04:28
-
-
Save attilaacs/190293e2c6bab5b39d4f243ce4edaaa2 to your computer and use it in GitHub Desktop.
Wordpress Admin: sort custom post type column by ACF date time field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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