0

I'm trying to exclude WooCommerce products from default WP search, but I need to keep all other posts types, including CPT and those CPT created in the future. Im trying with:

function searchFilter($query) {
    if (!$query->is_admin && $query->is_search) {
        $query->set('post_type', array('post', 'page', 'CPT'));
    }
    return $query;
}
add_filter('pre_get_posts', 'searchFilter');

In code above I can set in post_type what posts types I want to choose. But I would like to exclude only a 'products' post_type. Is there any way to create parameter like that?

1 Answer 1

0

If you want to exclude the "product" post type (= WooCommerce products) from all front-end searches on your site, you can use this code:

function my_adjust_post_type_args( $args, $post_type ) {
    if ( 'product' === $post_type ) {
        $args['exclude_from_search'] = true;
    }

    return $args;
}

add_filter( 'register_post_type_args', 'my_adjust_post_type_args', 10, 2 );

It modifies the "exclude_from_search" argument when the "product" post type is registered. See the docs for the register_post_type() function for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.