Last active
June 28, 2024 21:35
-
-
Save andrasguseo/ea6878ca8c0fef9a7e64b04925ec0170 to your computer and use it in GitHub Desktop.
TEC > Adding the event status column to the events admin and making it 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
<?php | |
/** | |
* Description: Adds the event status column to the events admin and makes it sortable. | |
* Usage: Paste the below snippet into your active (child) theme's functions.php file | |
* or use a plugin like Code Snippets. | |
* | |
* Plugins: The Events Calendar | |
* Author: Andras Guseo, Masood Ahmed Khan, Guga Alvez | |
* Last updated: 2024-06-28 | |
*/ | |
if ( class_exists( 'Tribe__Events__Main' ) ) { | |
add_filter( 'manage_tribe_events_posts_columns', 'custom_tec_statuses_columns' ); | |
function custom_tec_statuses_columns( $columns ) { | |
$columns['tribe-events-status'] = __( 'Event Status' ); | |
return $columns; | |
} | |
add_action( 'manage_tribe_events_posts_custom_column', 'custom_tec_statuses_column', 10, 2 ); | |
function custom_tec_statuses_column( $column, $post_id ) { | |
if ( 'tribe-events-status' === $column ) { | |
$event_status = get_post_meta( $post_id, '_tribe_events_status', true ) ?: 'Scheduled'; | |
$cleaned_event_status = ucwords( str_replace( '-', ' ', $event_status ) ); | |
echo $cleaned_event_status; | |
} | |
} | |
add_filter( 'manage_edit-tribe_events_sortable_columns', 'custom_ce_events_sortable_columns' ); | |
function custom_ce_events_sortable_columns( $columns ) { | |
$columns['tribe-events-status'] = 'tribe-events-status'; | |
return $columns; | |
} | |
// Add the join clause | |
function tribe_events_custom_join( $join ) { | |
global $wpdb; | |
if ( is_admin() && is_main_query() ) { | |
$join .= " LEFT JOIN {$wpdb->postmeta} AS tribe_events_status ON {$wpdb->posts}.ID = tribe_events_status.post_id AND tribe_events_status.meta_key = '_tribe_events_status'"; | |
} | |
return $join; | |
} | |
add_filter( 'posts_join', 'tribe_events_custom_join' ); | |
// Adjust the orderby clause in SQL | |
function tribe_events_custom_orderby( $orderby, $query ) { | |
global $wpdb; | |
// Bail, if not admin. | |
if ( ! is_admin() ) { | |
return $orderby; | |
} | |
// Bail, if not main query. | |
if ( ! $query->is_main_query() ) { | |
return $orderby; | |
} | |
// Bail, if it's not for events. | |
if ( $query->get( 'post_type' ) !== 'tribe_events' ) { | |
return $orderby; | |
} | |
$orderby_value = $query->get( 'orderby' ); | |
if ( 'tribe-events-status' == $orderby_value ) { | |
$orderby = "tribe_events_status.meta_value " . $query->get( 'order' ); | |
} | |
return $orderby; | |
} | |
add_filter( 'posts_orderby', 'tribe_events_custom_orderby', 20, 2 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment