While it’s possible to sort the announcements on the dashboard by the dates by clicking on the column title, it sorts by the published date out of the box. This means that each time you want to sort the announcements by the announcement date then you’re forced to re-click the column heading, and waiting for the page the reload.
One of our users asked in the support forum if it was possible to load this table sorted by the announcement date, so they didn’t have to constantly re-sort the table on every page load.
We thought that this was a great question and something that many of our users might be interested in implementing. So we went ahead and threw together a code snippet that you can add to your themes functions.php file or into a custom MU plugin.
Snippet
/**
* Sort the Timeline Express announcements by announcement date on the dashboard
* @see https://wordpress.org/support/topic/announcement-list-order/
*
* @param object $query Query object.
*/
function timeline_express_announcements_sort_by_date( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
$screen = get_current_screen();
if ( ! isset( $screen->post_type ) || 'te_announcements' !== $screen->post_type || isset( $_GET['orderby'] ) ) {
return;
}
$query->set( 'meta_key', 'announcement_date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' ); // Possible: ASC/DESC
}
add_action( 'pre_get_posts', 'timeline_express_announcements_sort_by_date' );
Parameters:
The code snippet above will load the announcements table and sort them by announcement date in ascending order, but will still allow you to sort the table by another value if you choose to. You’ll still be able to sort the table by the published date or alphabetically by the announcement name by clicking on any of the column headings.
Once added to your themes functions.php file or into a custom MU plugin you can save the file, and check to confirm that the announcements now load in the order you need.