Archived topic
How to style 2 different search results pages
3 replies · Started by John MacKenzie on February 18, 2021
ok so in my site we want the navigation search to only show products from woocommerce and ive accomplished that with this code
// set search in menu to only show products
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
<input type="hidden" name="post_type" value="product" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );
however the search we have on the blog pages i want to show a different sidebar and only show page search results.
i tried adding this code but it then messed up the search results from my code above so ive removed it for now.
//Exclude pages from WordPress Search
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}
im using content aware sidebars to set different sidebars for different pages but it seems the search results page is technically the same for both types of search?
any ideas?
thanks a lot!
John
Hi there,
to do that would require custom development, you would need to create another search template for each specific search results and a separate search form for those search requirements, some very basic info on that here:
https://www.isitwp.com/create-multiple-search-templates-for-custom-post-types/
thanks David.
those links are interesteing but beyond me so would have to get my programmer on that, but im not so sure actually, with content aware sidebars i already have 2 different looking search result pages with different search results now. Ive now got different sidebars set as i like, and last step is to simply limit the search results of the general blog search to only show blog posts? seems like this should be doable without the fancy programming?
ok i think i got it!
i added an exclusion for woocommerce in the if query-> is search line
and this appears to do what i want it to.
thanks to help from this post https://wordpress.org/support/topic/product-search-vs-post-search/
//Exclude pages from WordPress Search
if (!is_admin()) {
function wpb_search_filter($query) {
if ($query->is_search && !is_woocommerce()) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','wpb_search_filter');
}
// set search in menu to only show products
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
<input type="hidden" name="post_type" value="product" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );