Using search.php without a 's' field in searchform.php

Sorry if this is a simple/daft question but I'm still getting to grips with how WordPress search functions.

I want to completely replace the standard search within my template with a custom search that only queries a certain custom post type and its meta fields. I have a search form which does this and search.php which returns the correct data. However, the search will not function unless I include a input field named 's' and it is not empty. I take it WP needs this to access search.php?

I have no need for a search field as my search is based upon custom taxonomies and meta fields. So how can I still use my search without this field?

Is this possible?

Topic Wordpress search

Category Web


I think that you've made this hard on yourself. Without having all of the details I can't offer a working solution but I'd suggest using the s parameter instead of trying to get rid of it. That parameter is important, as you've noticed.

Use the parameter plus a filter on pre_get_posts to control the search, instead of completely hijacking it.

function alter_search_wpse_105761($qry) {
  if ($qry->is_search()) {
    $qry->set('post_type','your-post-type');
    // and other conditions
  }
}
add_action('pre_get_posts','alter_search_wpse_105761');

There are a lot of questions and answers here mentioning pre_get_posts so it should not be hard to work out the details, and you can edit the question with additional details to get more specific responses.


I am not sure as how to get around using id="s" in the input field. But just to clarify, I believe wordpress is doing something like $input = $_POST["s"]; when the search is submitted. The id="s" is necessary because it tells the php script from which html input field to pull the input from. Wordpress would then do all kinds of stuff to the $input variable and search the site for it. Not a solution, but hopefully resolves some confusion.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.