WordPress Default Search Functionality only searches all titles and content type data.
In this article learn how to search particular post type data.
Step-1:
=> Firstly We can add a search form using get_search_form() function.
Step-2:
=> We can use the pre_get_posts() action that lets you filter the post type data.
=> pre_get_posts() function that makes possible to modify existing WP_Query, and add new custom WP_Query in child theme functions.php file.
For example, the Below code searches only for Woocommece products data.
add_filter( 'pre_get_posts', 'custom_pre_get_posts' ); function custom_pre_get_posts( $query ) { if ( is_search() ) { $query->set('post_type', 'product'); } return $query; }
OutPut:
How to search multiple post-type data.
For example, the below code searches for multiple post type data:
add_filter('pre_get_posts', 'search_filter_multiple_post_type'); function search_filter_multiple_post_type( $query ) { if ( is_search() ){ $query->set( 'post_type', array( 'book', 'product' ) ); } return $query; }
OutPut: