Last active
March 13, 2020 12:41
-
-
Save woraperth/a27ff834963eefbf45c5685d7096baea to your computer and use it in GitHub Desktop.
[WordPress] Add ACF Admin Column to Custom Post Type (In this example, CPT name = events & ACF column name = event_date). This also make the column sortable.
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 ACF Columns | |
function add_new_events_column($columns) { | |
$columns['event_date'] = 'Event Date'; | |
return $columns; | |
} | |
add_filter('manage_events_posts_columns', 'add_new_events_column'); | |
function add_new_events_admin_column_show_value( $column, $post_id ) { | |
if ($column == 'event_date') { | |
$evdate = get_field('event_date'); | |
echo $evdate; | |
} | |
} | |
add_filter('manage_events_posts_custom_column', 'add_new_events_admin_column_show_value', 10, 2); | |
/* Make the column sortable */ | |
function set_custom_events_sortable_columns( $columns ) { | |
$columns['event_date'] = 'event_date'; | |
return $columns; | |
} | |
add_filter( 'manage_edit-events_sortable_columns', 'set_custom_events_sortable_columns' ); | |
function events_custom_orderby( $query ) { | |
if ( ! is_admin() ) | |
return; | |
$orderby = $query->get('orderby'); | |
if ( 'event_date' == $orderby ) { | |
$query->set( 'meta_key', 'event_date' ); | |
$query->set( 'orderby', 'meta_value' ); | |
} | |
} | |
add_action( 'pre_get_posts', 'events_custom_orderby' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment