Limit Search Form to Specific Post Types

If you’d like to limit the search form on WordPress site to specific post types globally, there’s and easy way to do that:

View this code snippet on GitHub.

But let’s say you want to have a specific search form search limited to one post type, but allow other search forms to search all post types?

Most tutorials I’ve run across suggest that you re-create all the search form markup and then add a hidden input to the form like this:

View this code snippet on GitHub.

You can then conditionally load that hidden field on the specific templates where where it is needed.

However, if you’re using get_search_form() and don’t want to completely replace all the search form markup, an alternative would be to do a string replace before outputting the form:

View this code snippet on GitHub.

For more reading on how this filter works, you can read the great inline docs in the WordPress codebase itself.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *