2

I have 5 custom post types in my website and i need to make a search form for each one to show only results from it. What should be in the search form to only show results from the custom post type which it came from? How do i redirect from each page to a specific page results?

<form role="search" method="get" action="/">
  <input type="hidden" id="cat" name="paints_buildings" />
  <input type="text" size="16" name="s" placeholder="Search"  />
  <input type="submit" value="Go" />
</form>

1 Answer 1

1

If you only want to search a particular post type include a hidden field with the name post_type and the value set as the name of the post type you want to search:

<form role="search" method="get" action="/">
    <input type="text" name="s" placeholder="Search">
    <input type="hidden" name="post_type" value="post_type_name">
    <input type="submit" value="Go">
</form>

Just replace post_type_name with the actual name of your post type.

Now when you search the URL will look like this:

http://example.com/?s=search+term&post_type=post_type_name

And only return results from that post type.

3
  • Forgot to mention but actually my search form is inside a single page so my URL is example.link/post-type/single-post-name/?s=search+term is there a way to avoid this?
    – Asaf Hadad
    Jul 30, 2018 at 10:58
  • Avoid what? Your URL or mine? Where the search form is has nothing to do with the results URL. The results URL is determined by the action attribute of the form. Jul 30, 2018 at 11:01
  • i was missing the name="post_type" and that's why i got results from other places too.. works great now! thanks.
    – Asaf Hadad
    Jul 30, 2018 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.