Skip to content

Instantly share code, notes, and snippets.

@mediaformat
Created May 12, 2020 15:53
Show Gist options
  • Save mediaformat/c3874ce60acdd858fb5bfa15011701f9 to your computer and use it in GitHub Desktop.
Save mediaformat/c3874ce60acdd858fb5bfa15011701f9 to your computer and use it in GitHub Desktop.
auto remove past events from search engines (nofollow + sitemap) fror wpseo + tribe
<?php
// gleaned from https://wordpress.org/support/topic/remove-past-events-from-xml-sitemap/#post-11431056
/* Modify the WP-SEO plugin's defaults */
if ( is_plugin_active( 'wordpress-seo/wp-seo.php' ) ) {
/* add nofollow metatag to header of past events */
add_filter("wpseo_robots", function($robots) {
if (! is_singular( 'exhibition' ) && tribe_is_past_event() == true) {
return "noindex,follow";
}
return $robots;
});
/* exclude past events from the sitemap */
function my_find_expired_events( $ids ) {
$args = array(
'post_type' => 'tribe_events',
'nopaging' => true,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_EventEndDate',
'value' => date( 'Y-m-d H:i:s' ),
'compare' => '<',
'type' => 'DATETIME',
),
),
);
$expired_events = get_posts( $args );
$ids = array_merge( $ids, $expired_events );
$ids = array_map( 'absint', $ids );
$ids = array_unique( $ids );
return $ids;
}
add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', 'my_find_expired_events' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment