Archived topic
How to exclude GP searching pages
11 replies · Started by Nacho on January 15, 2020
Hello,
How would you exclude the GP search funcionanility from searching pages?
Thanks a lot.
Hi there,
We will need this filter:
https://docs.generatepress.com/article/generate_navigation_search_output/
Do you only want to search for posts?
Let me know :)
Hi Leo,
Yes I just want to search for posts not pages.
Thanks!
Try this PHP snippet:
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="post" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );
Adding PHP: https://docs.generatepress.com/article/adding-php/
Hello Leo,
I´ve just put the code but it´s not working.
I´ve tried in the live version so I´ve updated this post website URL.
If you search for example the word "tour" it will show the tour page and that´s a page not a post.
Thanks a lot.
I'm not seeing the navigation search in the URL provided.
Can you double check?
Let me know :)
Hello Leo,
I´ve just updated the url so you can see the search box.
Thanks a lot.
Hi there,
It doesn't look like you're using the Navigation Search.
Are you wanting to target the search bar in your right sidebar?
Hi Tom,
Oh yes the search bar in the sidebar. Actually I thought it was the same.
What should I do?
Thanks a lot.
That's a WordPress search widget which is different than GP's navigation search:
https://www.wpbeginner.com/wp-tutorials/how-to-exclude-pages-from-wordpress-search-results/
Hi Leo,
Understood. That worked like charm.
Thanks a lot for the help to all of you and have a good day.
No problem :)