• Resolved dylan

    (@dylan)


    I have Yoast SEO installed as well as The Events Calendar. I’d like to use the filter to remove posts from the XML sitemap, whenever those posts are events that occurred in the past. So far, I’ve been able to find some filters and functions that should help with this. I’ve put the following into the funcitons.php file, but it doesn’t seem to work.

    
    //remove past events from yoast sitemap
    add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', function () {
       tribe_get_events(array('posts_per_page'=>-1, 'eventDisplay'=>'past') );
    } );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • MariusG

    (@marius_codeinwp)

    Hi Dylan,

    Are you trying to noindex all expired events (which would also remove them from the XML sitemaps)? If so, a better solution would be using the wpseo_robots filter.

    You can find a demo of the filter being used here.

    Plugin Support Michael Ti?a

    (@mikes41720)

    Setting to resolved as there is no further response.

    Thread Starter dylan

    (@dylan)

    If you only add NOINDEX meta, without also removing the items from the sitemap, you’ll send confusing instructions to a search engine, so you have to do both. This is what I came up with. It seems to work.

    /* 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' );
    
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Remove Past Events from XML Sitemap’ is closed to new replies.