{"id":1966220,"date":"2025-08-08T02:54:02","date_gmt":"2025-08-08T06:54:02","guid":{"rendered":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/?p=1966220"},"modified":"2025-08-08T02:54:03","modified_gmt":"2025-08-08T06:54:03","slug":"how-to-hide-events-that-have-already-started-in-the-events-calendar","status":"publish","type":"post","link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/how-to-hide-events-that-have-already-started-in-the-events-calendar\/","title":{"rendered":"How to Hide Events That Have Already Started in The Events Calendar"},"content":{"rendered":"\n<p>By default, The Events Calendar displays <strong>ongoing events<\/strong> even if they have already started, as long as they have not ended. This is useful for multi-day events, but in some cases, you might want to <strong>hide events as soon as they begin<\/strong>.<\/p>\n\n\n\n<p>In this guide, you\u2019ll learn how to prevent events from appearing on the main calendar views if their <strong>start date\/time is in the past<\/strong>. We\u2019ll show you how to use the <code>pre_get_posts<\/code> action to filter the main query and respect the site&#8217;s timezone settings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-use-case\">Use Case<\/h3>\n\n\n\n<p>This is helpful if:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You only want to show <strong>upcoming events<\/strong>.<\/li>\n\n\n\n<li>You don\u2019t want events to remain visible <strong>after they\u2019ve started<\/strong>.<\/li>\n\n\n\n<li>You need to make sure the comparison uses the <strong>correct timezone<\/strong> as set in WordPress.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-how-it-works\" style=\"font-size:25px\"><strong>How It Works<\/strong><\/h3>\n\n\n\n<p>We&#8217;ll hook into WordPress\u2019s <code>pre_get_posts<\/code> action and modify the main query that loads events. The logic ensures that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It only affects frontend views.<\/li>\n\n\n\n<li>It uses the <strong>site\u2019s configured timezone<\/strong>.<\/li>\n\n\n\n<li>It filters out events that started <strong>before the current time<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-php-snippet-hide-already-started-events-timezone-aware\" style=\"font-size:25px\"><strong>PHP Snippet: Hide Already Started Events (Timezone-Aware)<\/strong><\/h3>\n\n\n\n<p>Add the following code to your theme\u2019s <code>functions.php<\/code> file or use the <strong><a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" target=\"_blank\" rel=\"noreferrer noopener\">Code Snippets<\/a> <\/strong>plugin:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nadd_action( &#039;pre_get_posts&#039;, &#039;hide_already_started_events_using_timezone&#039; );\n\nfunction hide_already_started_events_using_timezone( $query ) {\n    \/\/ Only target the main query on the frontend\n    if ( is_admin() ) {\n        return;\n    }\n\n    \/\/ Get the current time in the site&#039;s timezone\n    $timezone_string = get_option( &#039;timezone_string&#039; );\n    $timezone = $timezone_string ? new DateTimeZone( $timezone_string ) : wp_timezone(); \/\/ fallback\n    $now = new DateTime( &#039;now&#039;, $timezone );\n    $today = $now-&gt;format( &#039;Y-m-d H:i:s&#039; );\n\n    \/\/ Modify the query to exclude events that have already started\n    $meta_query = $query-&gt;get( &#039;meta_query&#039; );\n    if ( ! is_array( $meta_query ) ) {\n        $meta_query = &#x5B;];\n    }\n\n    $meta_query&#x5B;] = &#x5B;\n        &#039;key&#039;     =&gt; &#039;_EventStartDate&#039;,\n        &#039;value&#039;   =&gt; $today,\n        &#039;compare&#039; =&gt; &#039;&gt;=&#039;,\n        &#039;type&#039;    =&gt; &#039;DATETIME&#039;,\n    ];\n\n    $query-&gt;set( &#039;meta_query&#039;, $meta_query );\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-result\" style=\"font-size:25px\"><strong>Result<\/strong><\/h3>\n\n\n\n<p>This will remove any events from calendar views if their start date\/time is in the past. Events will <strong>no longer be listed<\/strong> once they begin, even if they are still ongoing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-important-notes\">Important Notes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This affects <strong>all frontend event queries<\/strong>, including List, Month, and Day views.<\/li>\n\n\n\n<li>This does <strong>not<\/strong> affect single event pages. Events can still be viewed directly via their permalink.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>By default, The Events Calendar displays ongoing events even if they have already started, as long as they have not ended. This is useful for multi-day events, but in some cases, you might want to hide events as soon as they begin. In this guide, you\u2019ll learn how to prevent events from appearing on the&#8230;<\/p>\n","protected":false},"author":48,"featured_media":1955565,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_swpsp_post_exclude":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"ep_exclude_from_search":false,"footnotes":""},"categories":[24,59,79],"tags":[25,58],"stellar-product-taxonomy":[161],"class_list":["post-1966220","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizing","category-customizing-resources","category-snippets","tag-customizations","tag-php","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":24,"label":"Customizations"},{"value":59,"label":"PHP &amp; Functions"},{"value":79,"label":"Snippets"}],"post_tag":[{"value":25,"label":"Customizations"},{"value":58,"label":"PHP"}],"stellar-product-taxonomy":[{"value":161,"label":"The Events Calendar"}]},"featured_image_src_large":["https:\/\/images.theeventscalendar.com\/kb\/uploads\/2023\/02\/social-share-1024x538.png",1024,538,true],"author_info":{"display_name":"Paul Acido","author_link":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/author\/paulacido\/"},"comment_info":0,"category_info":[{"term_id":24,"name":"Customizations","slug":"customizing","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":110,"filter":"raw","term_order":"0","cat_ID":24,"category_count":110,"category_description":"","cat_name":"Customizations","category_nicename":"customizing","category_parent":0},{"term_id":59,"name":"PHP &amp; Functions","slug":"customizing-resources","term_group":0,"term_taxonomy_id":59,"taxonomy":"category","description":"","parent":24,"count":101,"filter":"raw","term_order":"0","cat_ID":59,"category_count":101,"category_description":"","cat_name":"PHP &amp; Functions","category_nicename":"customizing-resources","category_parent":24},{"term_id":79,"name":"Snippets","slug":"snippets","term_group":0,"term_taxonomy_id":79,"taxonomy":"category","description":"","parent":0,"count":136,"filter":"raw","term_order":"0","cat_ID":79,"category_count":136,"category_description":"","cat_name":"Snippets","category_nicename":"snippets","category_parent":0}],"tag_info":[{"term_id":25,"name":"Customizations","slug":"customizations","term_group":0,"term_taxonomy_id":25,"taxonomy":"post_tag","description":"","parent":0,"count":177,"filter":"raw","term_order":"0"},{"term_id":58,"name":"PHP","slug":"php","term_group":0,"term_taxonomy_id":58,"taxonomy":"post_tag","description":"","parent":20,"count":128,"filter":"raw","term_order":"0"}],"_links":{"self":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1966220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=1966220"}],"version-history":[{"count":4,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1966220\/revisions"}],"predecessor-version":[{"id":1966406,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1966220\/revisions\/1966406"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media\/1955565"}],"wp:attachment":[{"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=1966220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=1966220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=1966220"},{"taxonomy":"stellar-product-taxonomy","embeddable":true,"href":"https:\/\/staging.theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/stellar-product-taxonomy?post=1966220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}