I want search in specific multiple custom post types. I using second code to determine post types:
function custom_search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', array('post','product'));
}
}
}
add_action('pre_get_posts','custom_search_filter');
I can not search for woocommerce products, if in array post_type is more than only 'product' cpt. If is only 'product' then this post type is findable.
In my search.php file code is:
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post(); ?>
founded
<? endwhile;
endif;
?>
If in array('post','page'), all is working good too, posts and pages are findable.
WP_Query
object withWC_Query
when querying for products. Have you tried to add your action later?add_action('pre_get_posts','custom_search_filter', 90);