Remove Past Events from Yoast Sitemap

Using two wordpress plugins, Yoast SEO, and The Events Calendar PRO. How can I prevent Yoast’s XML sitemap from listing Calendar events that are in the past?

Yoast’s documentation provides one way to exclude posts from the sitemap. It requires an array of post IDs. Here’s the example code for functions.php:

// hide posts by id from Yoast Sitemap
add_filter( ‘wpseo_exclude_from_sitemap_by_post_ids’, function () {
return array( 311, 322 );
} );

The Events Calendar also provides a way to query for events. If I have this right, it would also go in functions.php and would provide a list of past events.

// Retrieve past events from The Events Calendar
$events = tribe_get_events( array(
‘eventDisplay’ => ‘past’
) );

Is there a way to combine these two things, so that the past events are prevented from the sitemap?

(p.s. just to reiterate, this question is specifically about removing the posts from the sitemap, not about deleting the posts or adding noindex metatags.)

Read more here:: Remove Past Events from Yoast Sitemap

Leave a Reply

Your email address will not be published. Required fields are marked *