How to filter WordPress search by custom post type

Add the input hidden field

If you want to limit the WordPress search to a specific custom post type, add a hidden input field with that post type:

<input type="hidden" name="post_type" value="books" />


This is a form with a search input field and a hidden field which will filter the results only for my "books" custom post type:

<form method="get" action="/" id="search" name="search">
       <input type="text" value="" name="s" id="s" placeholder="Search">
       <input type="hidden" name="post_type" value="books" />

About Ricard Torres

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.

@ricard_dev @ricard_dev

📝 Blog 🎙 Podcast

Leave a Reply

Add <code> Some Code </code> by using this tags.