0

I have a basic job board on a site (a custom WP theme) that uses the WP search form to search by keywords in the 'jobs' custom post type, and users can also click on job categories which I have listed in a column using get_the_term_list().

The client has mooted the idea of adding other search functionality - salary levels, geographical locations etc. If I was to set these up as their own taxonomies is there any built in functionality in WordPress where I can combine different criteria (taxonomies) in an html form and then use a submit button to return the data from all of the fields combined, or is this something that would need to be done natively with PHP?

I appreciate if this a PHP thing it might be a steep learning curve etc.

Many thanks in advance

1 Answer 1

2

You can do this with basic input fields and the URL.

This is because example.com/?s=test is a search URL for "test", but example.com/category/foo/?s=test also works, and searches the foo category.

Likewise, most of the other parameters WP_Query takes also work, e.g. post_type.

If we dig a bit deeper, all the pretty permalinks, and WP rewrite rules, are just regular expressions that convert pretty URLs, into the form index.php?s=test&cat=foo etc.

So create a form, give it inputs with the names and values that match the keys and values of WP_Query, and make sure it uses GET as the method so they appear in the URL.

For example, this search form searches a custom post type:

<form method="get" action="/">
    <input type="hidden" name="post_type" value="mycpt" />
    <input type="text" name="s" placeholder="search mycpts"/>
    <input type="submit" value="search"/>
</form>

Notice that there is an input field with post_type.

You can expand this with any of the standard HTML inputs, text boxes, select dropdowns, etc as long as the name matches a value WP_Query takes

A note however, WP search isn't great, and the options for improving it are limited. You may also be tempted to search for posts via their meta values or add exclusions, resist this temptation! The performance cost of filtering/searching for posts by their post meta values is huge. It's so huge they built a completely separate system of tables for doing it called taxonomies and terms.

For further reading, look at the official docs for WP_Query https://developer.wordpress.org/reference/classes/wp_query/

2
  • Hi Tom, yes my query is about doing it with taxonomies and/or terms, e.g. having a <select> form element with salary bands which are already in the database as custom taxonomies and then the user can select from another set of custom taxonomies (e.g. job title) and the can then hit the submit button and the query returns jobs that match the title and salary. I'm guessing this will have to be done with native PHP / SQL and there won't be any WP functionality for this?
    – pjk_ok
    Feb 11, 2021 at 0:50
  • 1
    Definitely no SQL ( writing SQL in WP dev is usually a bad sign ) this can be done entirely using HTML input tags. You may need to write some PHP to retrieve a list of taxonomy terms to build your input html, but that's a separate and different question
    – Tom J Nowell
    Feb 11, 2021 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.