admin管理员组文章数量:1332395
When you use the default WordPress search functionality you can only search on Title, so if you type in the title than the posts you search for will appear in the results. I would like to expand this by also being able to search on date, category and author.
I tried to look it up but what I mostly find is to search between dateranges or build a custom searchpage. This is however not what I am trying to accomplish. I just want the normal searchbar that puts the ?s=mysearch in the url to be able to also search on this three things.
Is this possible to extend somehow or do I really need to build a custom searchpage for this?
Thanks!
When you use the default WordPress search functionality you can only search on Title, so if you type in the title than the posts you search for will appear in the results. I would like to expand this by also being able to search on date, category and author.
I tried to look it up but what I mostly find is to search between dateranges or build a custom searchpage. This is however not what I am trying to accomplish. I just want the normal searchbar that puts the ?s=mysearch in the url to be able to also search on this three things.
Is this possible to extend somehow or do I really need to build a custom searchpage for this?
Thanks!
Share Improve this question asked Jul 8, 2020 at 9:05 MaartjeMaartje 5471 gold badge9 silver badges23 bronze badges 1- Just to clarify your question - do you want the one search box to be able to search on all 3 types of things? E.g. whatever you enter it should do a regular text search, then if it's a valid date to a date search, then try a category search too? Or are you thinking something else? – mozboz Commented Jul 8, 2020 at 9:42
2 Answers
Reset to default 1By default, the WordPress search gives an omni-like search facility to WP Posts and Pages that should search any built-in WP content. This can be further extended through a plugin such as Relevanssi.
You can though, if I'm understanding you correctly, create a custom search form where you can dictate the fields available to search on.
You would do this first of all by creating a new file in your theme's root titled something like mysearch-searchform.php
. This is just a HTML form, so whatever you put in will work in theory. The one must that I'm aware of is a hidden field to tell WP what post type you want to search within. So you'd use something like:
<input type="hidden" name="search" value="mycpt">
To search for news posts only. So, you now have your custom search form, which you can call in your theme by using:
<?php get_template_part( 'mycpt', 'searchform' ); ?>
So, you have your custom form, and it's outputting onto your page. Now you need to deal with the results. For that you will look to your functions.php
where I use the below function to tell WP what results file to load depending on the search form used:
function load_search_template_results(){
if(isset($_GET['search'])) {
if( $_GET['search'] == 'mycpt' ) {
load_template( locate_template( 'mycpt-search-result.php' ));
} else {
load_template( locate_template( 'search.php' ));
}
}
}
add_action('init','load_search_template_results');
The page mycpt-search-result.php
is just a wp_query loop, looping through post data as required to tailor to your design.
Hope that puts you on the right track.
When you use the default WordPress search functionality you can only search on Title
No.
WP_Query support search by multiply parameters. Include author, dates, categories.
You can check how it working in WP_Query->parse_query
and WP_Query->get_posts
and build your requests like
/?s=some&author=1&year=2020&monthnum=10
本文标签: Search functionalityinclude datecategory and author search
版权声明:本文标题:Search functionality - include date, category and author search 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282589a2446366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论