{"id":1953550,"date":"2022-07-08T10:27:56","date_gmt":"2022-07-08T14:27:56","guid":{"rendered":"https:\/\/theeventscalendar.com\/knowledgebase\/?post_type=tribe-knowledgebase&#038;p=1953550"},"modified":"2023-09-15T13:35:36","modified_gmt":"2023-09-15T17:35:36","slug":"exclude-events-from-wordpress-search-results","status":"publish","type":"post","link":"https:\/\/theeventscalendar.com\/knowledgebase\/exclude-events-from-wordpress-search-results\/","title":{"rendered":"Exclude Events from WordPress Search Results"},"content":{"rendered":"\n<p>By default, events created with <a href=\"https:\/\/theeventscalendar.com\/products\/wordpress-events-calendar\/\" target=\"_blank\" rel=\"noreferrer noopener\">The Events Calendar and Events Calendar Pro<\/a> appear in the WordPress search results on your website. However, the situation may arise where you&#8217;d like to exclude events from the WordPress search results instead. For example, perhaps you&#8217;d only like users to see your blog posts there.<\/p>\n\n\n\n<p>Don&#8217;t worry; you can add a snippet to your WordPress site to make that happen. Let&#8217;s see how to exclude events from WordPress search results below!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-the-snippet\">The snippet<\/h2>\n\n\n\n<p>Add the following snippet to your theme&#8217;s functions.php file or use this handy <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" target=\"_blank\" rel=\"noreferrer noopener\">Code Snippets plugin<\/a>:<\/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;my_search_exclude_filter&#039; );\nfunction my_search_exclude_filter( $query ) {\n    if ( ! $query-&gt;is_admin &amp;&amp; $query-&gt;is_search &amp;&amp; $query-&gt;is_main_query() ) {\n        $searchable_post_types = get_post_types( array( &#039;exclude_from_search&#039; =&gt; false ) );\n        $post_type_to_remove = &#039;tribe_events&#039;;\n        if( is_array( $searchable_post_types ) &amp;&amp; in_array( $post_type_to_remove, $searchable_post_types ) ) {\n            unset( $searchable_post_types&#x5B; $post_type_to_remove ] );\n            $query-&gt;set( &#039;post_type&#039;, $searchable_post_types );\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Exclude Past Events From WordPress Search Results<\/h3>\n\n\n\n<p>If you have a recurring event or multiple recurring events that have already occurred, they can cause the search results to become muddled. Use the following code snippet to prevent past events from appearing in search results.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\nfunction filter_search_results( $query ) {\n    if ( is_admin() ) {\n        return;\n    }\n\n    if ( ! $query-&gt;is_main_query() ) {\n        return;\n    }\n\n    if ( ! $query-&gt;is_search ) {\n        return;\n    }\n\n    $meta_query = &#x5B;\n        &#039;relation&#039; =&gt; &#039;or&#039;,\n        &#x5B;\n             &#039;key&#039;     =&gt; &#039;_EventEndDate&#039;,\n             &#039;value&#039;   =&gt; date(&#039;Y-m-d&#039;).&#039; 00:00:00&#039;,\n             &#039;compare&#039; =&gt; &#039;&gt;=&#039;,\n             &#039;type&#039;    =&gt; &#039;DATETIME&#039;\n\n        ],\n        &#x5B;\n             &#039;key&#039;     =&gt; &#039;_EventStartDate&#039;,\n             &#039;compare&#039; =&gt; &#039;NOT EXISTS&#039;,\n        ],\n   ];\n\n   $query-&gt;set( &#039;meta_query&#039;, $meta_query );\n\n}\n\nadd_filter(&#039;pre_get_posts&#039;, &#039;filter_search_results&#039;);\n<\/pre><\/div>\n\n\n<p>You can learn more about <a href=\"https:\/\/theeventscalendar.com\/knowledgebase\/k\/best-practices-for-implementing-custom-code-snippets\/\" target=\"_blank\" rel=\"noreferrer noopener\">using custom code snippets<\/a> in our Knowledgebase.<\/p>\n\n\n\n<p> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, events created with The Events Calendar and Events Calendar Pro appear in the WordPress search results on your website. However, the situation may arise where you&#8217;d like to exclude events from the WordPress search results instead. For example, perhaps you&#8217;d only like users to see your blog posts there. Don&#8217;t worry; you can&#8230;<\/p>\n","protected":false},"author":3,"featured_media":1955565,"comment_status":"closed","ping_status":"closed","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":[59,108],"tags":[25,58],"stellar-product-taxonomy":[161],"class_list":["post-1953550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-function-snippets","category-wordpress","tag-customizations","tag-php","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":59,"label":"PHP Functions &amp; Snippets"},{"value":108,"label":"WordPress"}],"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":"Jaime Marchwinski","author_link":"https:\/\/theeventscalendar.com\/knowledgebase\/author\/jaimetri-be\/"},"comment_info":0,"category_info":[{"term_id":59,"name":"PHP Functions &amp; Snippets","slug":"php-function-snippets","term_group":0,"term_taxonomy_id":59,"taxonomy":"category","description":"","parent":24,"count":127,"filter":"raw","term_order":"0","cat_ID":59,"category_count":127,"category_description":"","cat_name":"PHP Functions &amp; Snippets","category_nicename":"php-function-snippets","category_parent":24},{"term_id":108,"name":"WordPress","slug":"wordpress","term_group":0,"term_taxonomy_id":108,"taxonomy":"category","description":"","parent":0,"count":17,"filter":"raw","term_order":"0","cat_ID":108,"category_count":17,"category_description":"","cat_name":"WordPress","category_nicename":"wordpress","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":102,"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":78,"filter":"raw","term_order":"0"}],"_links":{"self":[{"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1953550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/comments?post=1953550"}],"version-history":[{"count":7,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1953550\/revisions"}],"predecessor-version":[{"id":1963563,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1953550\/revisions\/1963563"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media\/1955565"}],"wp:attachment":[{"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/media?parent=1953550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/categories?post=1953550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/tags?post=1953550"},{"taxonomy":"stellar-product-taxonomy","embeddable":true,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/stellar-product-taxonomy?post=1953550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}