Last active
October 8, 2024 22:12
-
-
Save andrasguseo/666cc4714fd8ce2ce9f324759bc73482 to your computer and use it in GitHub Desktop.
TEC > Button to show future events only
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 | |
/** | |
* This will add a button next to the bulk actions. | |
*/ | |
add_action( 'restrict_manage_posts', 'filter_future_events' ); | |
function filter_future_events() { | |
global $typenow; | |
// Check that we're on the correct post type | |
if ( $typenow == 'tribe_events' ) { | |
?> | |
<button type="submit" name="future_events" class="button" value="1">Future Events Only</button> | |
<?php | |
} | |
} | |
add_action( 'pre_get_posts', 'filter_events_query_by_future_date' ); | |
function filter_events_query_by_future_date( $query ) { | |
global $pagenow, $typenow; | |
// Only modify the query on the tribe_events admin page | |
if ( $pagenow == 'edit.php' && $typenow == 'tribe_events' && isset( $_GET['future_events'] ) && $_GET['future_events'] == '1' ) { | |
$meta_query = array( | |
array( | |
'key' => '_EventStartDate', | |
'value' => current_time( 'Y-m-d H:i:s' ), // Compare with the current date and time | |
'compare' => '>', | |
'type' => 'DATETIME' | |
) | |
); | |
$query->set( 'meta_query', $meta_query ); | |
} | |
} |
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 | |
/** | |
* This will add a filter link next to "All, Published, Draft ..." for future and past events. | |
*/ | |
add_filter( 'views_edit-tribe_events', 'tec_custom_post_status_views_future_and_past_agu' ); | |
function tec_custom_post_status_views_future_and_past_agu( $views ) { | |
global $typenow; | |
// Check if we're on the custom post type's list page | |
if ( $typenow == 'tribe_events' ) { | |
// Add a custom status filter link for future events. | |
$future_count = tribe_events() | |
->where( 'starts_on_or_after', 'now' ) | |
->where( 'status', [ 'publish', 'private', 'pending', 'draft' ] ) | |
->per_page( - 1 ) | |
->count(); | |
$future_url = add_query_arg( | |
[ | |
'future_events' => '1', | |
'post_type' => 'tribe_events' | |
], | |
'edit.php' | |
); | |
$future_class = ( isset( $_GET['future_events'] ) && $_GET['future_events'] ) ? 'current' : ''; | |
$views['future_events'] = sprintf( | |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', | |
$future_url, | |
$future_class, | |
'Future Events', | |
$future_count | |
); | |
// Add a custom status filter link for past events. | |
$past_count = tribe_events() | |
->where( 'ends_before', 'now' ) | |
->where( 'status', [ 'publish', 'private', 'pending', 'draft' ] ) | |
->per_page( - 1 ) | |
->count(); | |
$past_url = add_query_arg( | |
[ | |
'past_events' => '1', | |
'post_type' => 'tribe_events', | |
], | |
'edit.php' | |
); | |
/** | |
* Only highlight past events if selected, and future events are NOT selected. | |
* (Edge case when manually adding both to the URL.) | |
*/ | |
$past_class = ( isset( $_GET['past_events'] ) && $_GET['past_events'] && $future_class == '' ) ? 'current' : ''; | |
$views['past_events'] = sprintf( | |
'<a href="%s" class="%s">%s <span class="count">(%d)</span></a>', | |
$past_url, | |
$past_class, | |
'Past Events', | |
$past_count | |
); | |
} | |
return $views; | |
} | |
add_action( 'pre_get_posts', 'tec_filter_events_query_by_future_date_agu' ); | |
function tec_filter_events_query_by_future_date_agu( $query ) { | |
global $pagenow, $typenow; | |
// Bail if not the edit page. | |
if ( $pagenow != 'edit.php' ) { | |
return; | |
} | |
// Bail if not events. | |
if ( $typenow != 'tribe_events' ) { | |
return; | |
} | |
// Bail if not the main query. | |
if ( ! $query->is_main_query() ) { | |
return; | |
} | |
// Modify query for future events. | |
if ( | |
isset( $_GET['future_events'] ) | |
&& $_GET['future_events'] == '1' | |
) { | |
$meta_query = array( | |
array( | |
'key' => '_EventStartDate', | |
'value' => current_time( 'Y-m-d H:i:s' ), // Compare with the current date and time | |
'compare' => '>', | |
'type' => 'DATETIME' | |
) | |
); | |
$query->set( 'meta_query', $meta_query ); | |
return; | |
} | |
// Modify query for past events. | |
if ( | |
isset( $_GET['past_events'] ) | |
&& $_GET['past_events'] == '1' | |
&& $_GET['future_events'] != '1' | |
) { | |
$meta_query = array( | |
array( | |
'key' => '_EventEndDate', | |
'value' => current_time( 'Y-m-d H:i:s' ), // Compare with the current date and time | |
'compare' => '<', | |
'type' => 'DATETIME' | |
) | |
); | |
$query->set( 'meta_query', $meta_query ); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment