1

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?

  • Could you just make the s field <input type="hidden" />? – Dominic P Jul 8 '13 at 20:20
  • I could but it still needs a value for the search to work. I dont want it in my code at all as I'm not using it and if its hidden there's nothing to stop people using dev tools to change its value. – James J Jul 9 '13 at 7:31
  • I can understand not wanting it in your code if it's not needed, but realize there's also nothing to stop people from entering yoursite.com?s=my+search directly which will accomplish the same thing as modifying a form input using dev tools. – Dominic P Jul 10 '13 at 4:38
0

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.

  • You probably mean name="s" right? I don't think the id attribute matters in the $_POST super global. – Dominic P Jul 8 '13 at 20:17
  • hmm. yes, I think you are right. – Vigs Jul 8 '13 at 20:23
0

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.

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.