Keep Past Events Out of the XML Sitemap
Leaves events that have already ended out of WordPress's built-in sitemap (wp-sitemap.xml), so search engines spend their time on upcoming events.
Opens WPIDE on
Code
add_filter( 'wp_sitemaps_posts_query_args', function ( $args, $post_type ) {
if ( 'tribe_events' !== $post_type ) {
return $args;
}
$args['meta_query'] = array( // phpcs:ignore WordPress.DB.SlowDBQuery
array( 'key' => '_EventEndDate', 'value' => current_time( 'mysql' ), 'compare' => '>=', 'type' => 'DATETIME' ),
);
return $args;
}, 10, 2 );
Notes
Only affects the core WordPress sitemap; SEO plugins with their own sitemaps have separate settings.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.