最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Is There Any Built In WP Functionality For Combining Form Fields To Return Specific Data

programmeradmin2浏览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

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

Share Improve this question asked Feb 11, 2021 at 0:26 pjk_okpjk_ok 9082 gold badges15 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

This is because example/?s=test is a search URL for "test", but example/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/reference/classes/wp_query/

发布评论

评论列表(0)

  1. 暂无评论