Last active
May 2, 2024 14:22
-
-
Save taricco/edca54040c3de940b16114c35427cad3 to your computer and use it in GitHub Desktop.
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
/*** TODAY VERSION - Custom function for making the /matchday/ URL redirect to the next upcoming match URL | |
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/ | |
add_action('template_redirect', 'redirect_to_next_match'); | |
function redirect_to_next_match() { | |
if (is_page('matchday')) { // Check if the current page is /matchday/ | |
$today = date('Ymd'); // Get today's date in ACF's preferred format. | |
$args = array( | |
'post_type' => 'match', // Your custom post type. | |
'posts_per_page' => 1, // We only need the next match. | |
'meta_key' => 'match_date', // The ACF field key for the match date. | |
'orderby' => 'meta_value_num', | |
'order' => 'ASC', // Order by the match date ascending. | |
'meta_query' => array( | |
array( | |
'key' => 'match_date', | |
'value' => $today, | |
'compare' => '>=', // Find matches with the date today or in the future. | |
'type' => 'DATE' | |
), | |
), | |
); | |
$matches = new WP_Query($args); | |
if ($matches->have_posts()) { | |
while ($matches->have_posts()) { | |
$matches->the_post(); | |
$match_url = get_the_permalink(); | |
wp_redirect($match_url); // Redirect to the match page. | |
exit; | |
} | |
} | |
} | |
} | |
/*** TOMORROW VERSION - Custom function for making the /matchday/ URL redirect to the next upcoming match URL | |
–––––––––––––––––––––––––––––––––––––––––––––––––– ***/ | |
add_action('template_redirect', 'redirect_to_next_match'); | |
function redirect_to_next_match() { | |
if (is_page('matchday')) { // Check if the current page is /matchday/ | |
$tomorrow = date('Ymd', strtotime('+1 day')); // Get tomorrow's date in ACF's preferred format. | |
$args = array( | |
'post_type' => 'match', // Your custom post type. | |
'posts_per_page' => 1, // We only need the next match. | |
'meta_key' => 'match_date', // The ACF field key for the match date. | |
'orderby' => 'meta_value_num', | |
'order' => 'ASC', // Order by the match date ascending. | |
'meta_query' => array( | |
array( | |
'key' => 'match_date', | |
'value' => $tomorrow, | |
'compare' => '>=', // Find matches with the date tomorrow or in the future. | |
'type' => 'DATE' | |
), | |
), | |
); | |
$matches = new WP_Query($args); | |
if ($matches->have_posts()) { | |
while ($matches->have_posts()) { | |
$matches->the_post(); | |
$match_url = get_the_permalink(); | |
wp_redirect($match_url); // Redirect to the match page. | |
exit; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment