How to search only for specific content types with the Elementor search form?

27.09.2022

Picture of a beautifully tidy garden

Elementor Pro has a widget called "Search Form". This is suitable for the pretty design of search forms. Unfortunately, this search form is limited in terms of programming.

Question: How can I modify the Elementor search form to restrict the search to certain content types (e.g. search only for Posts, Pages, Products, FAQ, ...)?

Answer / Solution:

  1. Give the widget a unique ID, e.g. mnc_search_posts
  2. Insert the following code into functions.php:
    add_action( 'elementor_pro/search_form/after_input', function( \ElementorPro\Modules\ThemeElements\Widgets\Search_Form $form ) {
       if($form->get_settings()['_element_id'] ?? null === 'mnc_search_posts') {
          echo '';
       }
    }, 10, 1 );

See also

No contributions found