Mark empty events page as no-index

#193201
  • Resolved Matthias
    Rank Math free

    Hi, we are using The Events Calendar Pro for events on our page, but have the problem, that empty days are indexed. Is it possible to mark empty event-views as no-index?

    Thank you!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Prabhat
    Rank Math agency

    Hello,

    Thanks for contacting us and we regret the inconvenience caused.

    I’m not sure whether I fully understand the question.

    Could you please confirm whether you’ve created the events page, the URL of which you’ve shared in the sensitive data section?

    If yes, then you can edit that page, and under the Advanced tab of Rank Math’s SEO Meta box, you can mark that page as No Index. https://i.rankmath.com/EnZ1UV

    Looking forward to hearing from you.

    Thank you.

    Matthias
    Rank Math free

    No this page is dynamically created by the Events Calendar Plugin

    Brian
    Rank Math free

    Hello,

    The noindex attribute is added to prevent Search bots from crawling your site pages.

    If all you want is for such URLs to be excluded from SERPs, you can disallow bots from indexing query strings by placing directives that specify that in your robots.txt file.

    You can go to WordPress Dashboard > Rank Math > General Settings > Edit Robots.txt and add the following text in there:

    User-Agent: *
    Disallow: /*?slug*

    That will prevent bots from seeing and hopefully not indexing all URLs that contain the specific slug in them. Please refer to more here:https://developers.google.com/search/docs/advanced/robots/robots-faq

    Please also note that blocking Google from crawling a page is likely to remove the page from Google’s index. However, robots.txt Disallow does not guarantee that a page will not appear in the results.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Matthias
    Rank Math free

    Hmm, therefore I would need to mark all days with no events in this file. Not very convenient. Isn’t there a filter or hook which I can trigger, when there are no events on a certain day that this particular page doesn’t show up in the sitemap and has no-index set?

    Ankit
    Rank Math business

    Hello,

    Please apply this filter code inside your function.php file :

    /**
     * Allows filtering of the robots meta data.
     *
     * @param array $robots The meta robots directives.
     */
    add_filter('rank_math/frontend/robots', function ($robots) {
    
        if (tribe_is_day($robots)) {
            $robots['index'] = "noindex";
            $robots['follow'] = "nofollow";
    
            return $robots;
        };
    
        return $robots;
    });

    Looking forward to helping you. Thank you.

    Matthias
    Rank Math free

    I think we are almost there. This checks if it’s a day view but not if that day has events. Do you know how to query if this event day has no events?

    Thank you!

    Ankit
    Rank Math business

    Hello,

    Thank you for your reply.

    I’d suggest you contact your plugin developer and ask them if they have any function that determines if tribe_is_day & has events inside the single-day view page. We can conditionally add that function inside our filter hook.

    I hope that helps. Thank you.

    Matthias
    Rank Math free

    Hi, I contacted them and they helped me a bit, but we are not there yet.

    We now have this code, but it actually never goes into the rank_math/frontend/robots filter and maybe you know why?

    add_action( 'init', 'rank_math_it' );
    
    function rank_math_it() {
    	remove_action( 'init', 'rank_math_it' );
    	add_filter( 'tribe_events_views_v2_view_data', 'tec_calendar_view', 10, 3 );
    }
    
    function tec_calendar_view( $data, $view_slug, $view ) {
    	// Bail If not day view
    	if ( $view_slug != 'day' ) {
    		return $data;
    	}
    
    	if ( empty ( $data['events'] ) ) {
    		// Do this if there are no events
    		add_filter('rank_math/frontend/robots', function ($robots) {
    			$robots['index'] = "noindex";
    			$robots['follow'] = "nofollow";
    			return $robots;
    		});
    		
    	} else {
    		// Do this if there are events
    
    	}
    	return $data;
    }

    Thank you!

    Hello,

    You will have to add the Rank Math filter without wrapping it in the Event calendar filter callback.

    Please contact the plugin author and ask them if there is any code to get the empty days’ value using post ID.

    You can then use code like the following to set robots to noindex.

    
    add_filter('rank_math/frontend/robots', function ($robots) {
        if ( ! is_singular() ) {
            return $robots;
        }
    
        global $post;
        
        $is_empty_days = event_calendar_function_to_get_days_value( $post->ID );
        
        if ( $is_empty_days ) {
                $robots['index'] = "noindex";
                $robots['follow'] = "nofollow";
        }
    
        return $robots;
    });
    

    I hope that helps.

    Matthias
    Rank Math free

    I contacted them and they said:

    This is not possible as a day does not have a post ID. Events and posts do.

    This is not possible as a day does not have a post ID. Events and posts do.

    The below snippet works and runs the code that is added below Do something.

    However, I tried to run the rank math filter there and that doesn’t want to run, not sure why. My thinking is that the rank math filter runs too early for this. So this will not work for this scenario.

    function filter_whatevs( $template_vars, $view ) {
    if ( empty( $template_vars[‘events’] ) ) {
    // Do something.
    }
    return $template_vars;
    }
    add_filter( ‘tribe_events_views_v2_view_day_template_vars’, ‘filter_whatevs’, 10, 2 );

    I’m not sure if this is possible. At the point when the rank math filter runs we don’t yet have data of the day.

    Hello,

    We use the wp_head hook to add the robots data so it might not run the Events filter.

    Since the event date value is passed to the URL, you can use PHP’s date function to compare it with the current date. Please try adding the following code to your theme’s functions.php file:

    
    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    	$url = $_SERVER['REQUEST_URI'];
    	$data = array_filter( explode( '/', $url ) );
    	$date = end( $data );
    	if (  false !== DateTime::createFromFormat( 'Y-m-d', $date ) && strtotime( $date ) < strtotime( 'today' ) ) {
    		$robots['index']  = 'noindex';
    		$robots['follow'] = 'nofollow';
    	}
    
    	return $robots;
    } );
    

    I hope that helps.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 11 replies - 1 through 11 (of 11 total)

The ticket ‘Mark empty events page as no-index’ is closed to new replies.