{"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":"2026-04-23T13:56:07","modified_gmt":"2026-04-23T17:56:07","slug":"customize-search","status":"publish","type":"post","link":"https:\/\/theeventscalendar.com\/knowledgebase\/customize-search\/","title":{"rendered":"Customizing Search for Events and Tickets"},"content":{"rendered":"\n<p>The Events Calendar and Event Tickets interact with search in two separate places: the main WordPress search on your site, and the event search bar that appears on calendar pages. This article covers snippets for customizing both.<\/p>\n\n\n\n<p>Customizations for The Events Calendar are usually implemented via code snippets or template overrides. Add snippets to your child theme&#8217;s <code>functions.php<\/code> file or use the Code Snippets plugin. Template overrides should go in a child theme. If either approach is new to you, start with <a href=\"https:\/\/theeventscalendar.com\/knowledgebase\/best-practices-for-implementing-custom-code-snippets\/\">Using Code Snippets to Customize The Events Calendar<\/a> and <a href=\"https:\/\/theeventscalendar.com\/knowledgebase\/customizing-template-files\/\">Customizing The Events Calendar Templates<\/a> for a walkthrough of each.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exclude-events-from-site-search\">Exclude Events from Site Search<\/h2>\n\n\n\n<p>By default, events created with <a href=\"https:\/\/theeventscalendar.com\/products\/wordpress-events-calendar\/\">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<pre class=\"wp-block-code\"><code>add_action( 'pre_get_posts', 'my_search_exclude_filter' );\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( 'exclude_from_search' =&gt; false ) );\n        $post_type_to_remove = 'tribe_events';\n        if( is_array( $searchable_post_types ) &amp;&amp; in_array( $post_type_to_remove, $searchable_post_types ) ) {\n            unset( $searchable_post_types&#91; $post_type_to_remove ] );\n            $query-&gt;set( 'post_type', $searchable_post_types );\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-exclude-past-events\">Exclude Past Events<\/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\n<pre class=\"wp-block-code\"><code>&lt;?php\nfunction filter_search_results( $query ) {\n    if ( is_admin() ) {\n        return;\n    }\n    if ( ! $query-&gt;is_main_query() ) {\n        return;\n    }\n    if ( ! $query-&gt;is_search ) {\n        return;\n    }\n    $meta_query = &#91;\n        'relation' =&gt; 'or',\n        &#91;\n             'key'     =&gt; '_EventEndDate',\n             'value'   =&gt; date('Y-m-d').' 00:00:00',\n             'compare' =&gt; '&gt;=',\n             'type'    =&gt; 'DATETIME'\n        ],\n        &#91;\n             'key'     =&gt; '_EventStartDate',\n             'compare' =&gt; 'NOT EXISTS',\n        ],\n   ];\n   $query-&gt;set( 'meta_query', $meta_query );\n}\nadd_filter('pre_get_posts', 'filter_search_results');<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-exclude-tickets-from-site-search\">Exclude Tickets from Site Search<\/h2>\n\n\n\n<p>By default, tickets created with <a href=\"https:\/\/theeventscalendar.com\/products\/wordpress-event-tickets\/\">Event Tickets Plus<\/a> and WooCommerce appear in the WordPress search results on your website. This is because they are created as regular WooCommerce products. However, the situation may arise where you&#8217;d like to exclude tickets 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>The following snippet will remove <strong>all tickets<\/strong> from the default WordPress search results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'pre_get_posts', function ( $query ) {\n    if ( ! is_admin() &amp;&amp; $query-&gt;is_main_query() &amp;&amp; $query-&gt;is_search() ) {\n        \/\/ Modify the query to exclude ticket products by checking for '_ticket_end_date'\n        $meta_query = &#91;\n            &#91;\n                'key'     =&gt; '_ticket_end_date',\n                'compare' =&gt; 'NOT EXISTS', \/\/ Exclude any products with '_ticket_end_date' field\n            ],\n        ];\n        \/\/ Apply the meta_query to exclude ticket products\n        $query-&gt;set( 'meta_query', $meta_query );\n    }\n} );<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-exclude-past-tickets\">Exclude Past Tickets<\/h3>\n\n\n\n<p>If you have a lot of events, and tickets that are past their sell date, they can cause the search results to become muddled. Use the following code snippet to prevent only <strong>past tickets<\/strong> from appearing in search results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add_action( 'pre_get_posts', function ( $query ) {\n\tif ( ! is_admin() &amp;&amp; $query-&gt;is_main_query() &amp;&amp; $query-&gt;is_search() ) {\n\t\t$current_date = current_time( 'mysql' );\n\t\t\/\/ Modify the query to exclude past tickets\n\t\t$meta_query = &#91;\n\t\t\t'relation' =&gt; 'OR',\n\t\t\t\/\/ Include tickets that haven't expired\n\t\t\t&#91;\n\t\t\t\t'key'     =&gt; '_ticket_end_date',\n\t\t\t\t'value'   =&gt; $current_date,\n\t\t\t\t'compare' =&gt; '&gt;=',\n\t\t\t\t'type'    =&gt; 'DATETIME',\n\t\t\t],\n\t\t\t\/\/ Include non ticket products\n\t\t\t&#91;\n\t\t\t\t'key'     =&gt; '_ticket_end_date',\n\t\t\t\t'compare' =&gt; 'NOT EXISTS',\n\t\t\t],\n\t\t];\n\t\t$query-&gt;set( 'meta_query', $meta_query );\n\t}\n} );<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-narrow-the-event-calendar-search-bar-to-titles-only\">Narrow the Event Calendar Search Bar to Titles Only<\/h2>\n\n\n\n<p>The event search bar at the top of every calendar page lets visitors search through your events. By default, that search looks for the given string within the event title, the description, and the excerpt. If you&#8217;d like to adjust this search functionality so that the search only happens in event titles, the following snippet can help. It will exclude both the content and the excerpt from the search.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction tec_search_only_event_title( $search, WP_Query $wp_query ) {\n\t\/\/ Bail when no search term\n\tif ( empty( $search ) ) {\n\t\treturn $search;\n\t}\n\n\t\/\/ Bail when not searching events\n\tif ( $wp_query-&gt;get( &#039;post_type&#039; ) !== &#039;tribe_events&#039; ) {\n\t\treturn $search;\n\t}\n\n\tglobal $wpdb;\n\t$new_search = preg_replace( &quot;\/{$wpdb-&gt;posts}.post_(content|excerpt) LIKE (&#039;&#x5B;{}a-zA-Z0-9]+&#039;)\/&quot;, &quot;0&quot;, $search );\n\n\treturn $new_search;\n}\n\nadd_action( &#039;posts_search&#039;, &#039;tec_search_only_event_title&#039;, 10, 2 );\n<\/pre><\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Events Calendar and Event Tickets interact with search in two separate places: the main WordPress search on your site, and the event search bar that appears on calendar pages. This article covers snippets for customizing both. Exclude Events from Site Search By default, events created with The Events Calendar and Events Calendar Pro appear&#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":[24,59],"tags":[25,58],"stellar-product-taxonomy":[161],"class_list":["post-1953550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizing","category-php","tag-customizations","tag-php","stellar-product-taxonomy-the-events-calendar"],"acf":[],"taxonomy_info":{"category":[{"value":24,"label":"Customizing"},{"value":59,"label":"PHP"}],"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":24,"name":"Customizing","slug":"customizing","term_group":0,"term_taxonomy_id":24,"taxonomy":"category","description":"","parent":0,"count":67,"filter":"raw","term_order":"0","cat_ID":24,"category_count":67,"category_description":"","cat_name":"Customizing","category_nicename":"customizing","category_parent":0},{"term_id":59,"name":"PHP","slug":"php","term_group":0,"term_taxonomy_id":59,"taxonomy":"category","description":"","parent":24,"count":52,"filter":"raw","term_order":"0","cat_ID":59,"category_count":52,"category_description":"","cat_name":"PHP","category_nicename":"php","category_parent":24}],"tag_info":[{"term_id":25,"name":"Customizations","slug":"customizations","term_group":0,"term_taxonomy_id":25,"taxonomy":"post_tag","description":"","parent":0,"count":31,"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":22,"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":11,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1953550\/revisions"}],"predecessor-version":[{"id":1970061,"href":"https:\/\/theeventscalendar.com\/knowledgebase\/wp-json\/wp\/v2\/posts\/1953550\/revisions\/1970061"}],"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}]}}