Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taricco/edca54040c3de940b16114c35427cad3 to your computer and use it in GitHub Desktop.
Save taricco/edca54040c3de940b16114c35427cad3 to your computer and use it in GitHub Desktop.
/*** 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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment