Skip to main content
Commonmark migration
Source Link

For the main search page, you can use pre_get_posts with a conditional:

if($query->is_main_query() && $query->is_search && !is_admin()) {

So only if it's the main query, it's a search query, and you're not in the admin, adjust the query. Set whatever post types you DO want indexed [in the main search query][1]in the main search query.

Next step, setting up a custom search. Depending on how you want this set up, one option is to create a Page template that first checks for a query string (i.e. a submitted search). If it finds a $_GET['customsearch'] parameter, display search results by using a custom WP_Query to pull results from only your CPT. If not, then display a search form (with customsearch as the name of the input).

Another option would be to create a widget which you can stick into any sidebar on the website, and have the form action be a Page with a custom Page template such that if $_GET['customsearch'] is empty the template does a wp_redirect before outputting header/etc. (you can redirect to homepage or whatever page is most relevant), but if there is a parameter it displays search results (again with a custom WP_Query). [1]: https://wordimpress.com/exclude-custom-post-types-from-search-results/

For the main search page, you can use pre_get_posts with a conditional:

if($query->is_main_query() && $query->is_search && !is_admin()) {

So only if it's the main query, it's a search query, and you're not in the admin, adjust the query. Set whatever post types you DO want indexed [in the main search query][1].

Next step, setting up a custom search. Depending on how you want this set up, one option is to create a Page template that first checks for a query string (i.e. a submitted search). If it finds a $_GET['customsearch'] parameter, display search results by using a custom WP_Query to pull results from only your CPT. If not, then display a search form (with customsearch as the name of the input).

Another option would be to create a widget which you can stick into any sidebar on the website, and have the form action be a Page with a custom Page template such that if $_GET['customsearch'] is empty the template does a wp_redirect before outputting header/etc. (you can redirect to homepage or whatever page is most relevant), but if there is a parameter it displays search results (again with a custom WP_Query). [1]: https://wordimpress.com/exclude-custom-post-types-from-search-results/

For the main search page, you can use pre_get_posts with a conditional:

if($query->is_main_query() && $query->is_search && !is_admin()) {

So only if it's the main query, it's a search query, and you're not in the admin, adjust the query. Set whatever post types you DO want indexed in the main search query.

Next step, setting up a custom search. Depending on how you want this set up, one option is to create a Page template that first checks for a query string (i.e. a submitted search). If it finds a $_GET['customsearch'] parameter, display search results by using a custom WP_Query to pull results from only your CPT. If not, then display a search form (with customsearch as the name of the input).

Another option would be to create a widget which you can stick into any sidebar on the website, and have the form action be a Page with a custom Page template such that if $_GET['customsearch'] is empty the template does a wp_redirect before outputting header/etc. (you can redirect to homepage or whatever page is most relevant), but if there is a parameter it displays search results (again with a custom WP_Query).

Source Link
WebElaine
  • 9.7k
  • 1
  • 16
  • 27

For the main search page, you can use pre_get_posts with a conditional:

if($query->is_main_query() && $query->is_search && !is_admin()) {

So only if it's the main query, it's a search query, and you're not in the admin, adjust the query. Set whatever post types you DO want indexed [in the main search query][1].

Next step, setting up a custom search. Depending on how you want this set up, one option is to create a Page template that first checks for a query string (i.e. a submitted search). If it finds a $_GET['customsearch'] parameter, display search results by using a custom WP_Query to pull results from only your CPT. If not, then display a search form (with customsearch as the name of the input).

Another option would be to create a widget which you can stick into any sidebar on the website, and have the form action be a Page with a custom Page template such that if $_GET['customsearch'] is empty the template does a wp_redirect before outputting header/etc. (you can redirect to homepage or whatever page is most relevant), but if there is a parameter it displays search results (again with a custom WP_Query). [1]: https://wordimpress.com/exclude-custom-post-types-from-search-results/