I am trying to organise the custom post type ‘Event’ in a new site so that it displays the event date in the admin columns and is sortable by that field. By ‘event date’ I do not mean the date the post is published but the ACF date picker field in the post named ‘event_date’.
I have managed to make the columns appear as I wish using the following code:
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'event_date' => __( 'Event Date', 'trans' ),
'venue' => __( 'Venue', 'trans' ),
'city' => __( 'City', 'trans' ),
'artist' => __( 'Artist', 'trans' ),
);
return $cols;
}
add_filter( "manage_event_posts_columns", "change_columns" );
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "event_date":
echo get_post_meta( $post_id, 'event_date', true);
break;
case "venue":
echo get_post_meta( $post_id, 'venue_name', true);
break;
case "city":
echo get_post_meta( $post_id, 'city_&_country', true);
break;
case "artist":
echo get_post_meta( $post_id, 'artist', true);
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
I have attempted to make the date column sortable as follows:
add_filter( 'manage_edit-event_sortable_columns', 'my_sortable_event_column' );
function my_sortable_event_column( $columns ) {
$columns['event_date'] = 'event_date';
return $columns;
}
The problems I now have are the following:
1. Despite the date picker field being set to return and display DD/MM/YYYY I am seeing YYYYMMDD in the admin column, which is far from ideal.
2. Although the option to sort is visible (downward/upward arrowhead is present), the sort order is still happening by publication date and not by event_date.
How can I get the dates to display correctly and the posts to be ordered by the correct field?
ACF stores dates in the database as “YYYYMMDD”, since you are using get_post_meta(), you are bypassing ACF and it will not format you’re dates for you. You’ll need to do this yourself.
To make the columns actually order by this column you need to add a pre_get_posts filter to these posts in the admin, here’s a tutorial I found, and you can probably find others https://wpdreamer.com/2014/04/how-to-make-your-wordpress-admin-columns-sortable/
Thanks John,
I’m not sure how to reformat the date but this is a secondary issue to the sorting one, and that’s now fixed. Appreciate your help. The complete code works out as:
// Change the columns for the edit CPT screen
function change_columns( $cols ) {
$cols = array(
'cb' => '<input type="checkbox" />',
'event_date' => __( 'Event Date', 'trans' ),
'venue' => __( 'Venue', 'trans' ),
'city' => __( 'City', 'trans' ),
'artist' => __( 'Artist', 'trans' ),
);
return $cols;
}
add_filter( "manage_event_posts_columns", "change_columns" );
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "event_date":
echo get_post_meta( $post_id, 'event_date', true);
break;
case "venue":
echo get_post_meta( $post_id, 'venue_name', true);
break;
case "city":
echo get_post_meta( $post_id, 'city_&_country', true);
break;
case "artist":
echo get_post_meta( $post_id, 'artist', true);
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
// Make edit screen columns sortable
add_filter( 'manage_edit-event_sortable_columns', 'my_sortable_event_column' );
function my_sortable_event_column( $columns ) {
$columns['event_date'] = 'event_date';
$columns['artist'] = 'artist';
return $columns;
}
add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
case 'event_date':
$query->set( 'meta_key', 'event_date' );
$query->set( 'orderby', 'meta_value' );
break;
case 'artist':
$query->set( 'meta_key', 'artist' );
$query->set( 'orderby', 'meta_value' );
break;
}
}
}
I’ll keep looking for a date solution.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.