1

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!

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 Jul 8 '20 at 9:42
1

By 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.

0

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

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.