Home Forums Trusted Pro Search pop-up misbehavior… pull down does not work. Reply To: Search pop-up misbehavior… pull down does not work.

#3702
Andy
Keymaster

The dropdown allows you or your site visitors to search within a WooCommerce product category. It does not work for selecting within post types as you listed in 1, 2, 3 above.

OK, a better way for you achieve what you want and let you filter the search to search within specific post types.
Add this code to the child theme’s functions.php file (this overrides our WooCommerce search form with the standard WP search form)

function trusted_woocommerce_search_form() {
    get_search_form();
}

Now let’s assume you have a custom post type named recipe, add this code to the child theme’s functions.php file

function trusted_custom_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'recipe' ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );

this code filters the standard search to search within posts that are recipes (your custom post type).

If you wanted to expand the search to also search within other post types, you would simply include more post types in the post_type array.
For example, to search recipes, posts and products, the code would be like so:

function trusted_custom_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array( 'recipe', 'post', 'product' ) );
    }
    return $query;
}
add_filter( 'pre_get_posts', 'trusted_custom_search_filter' );