WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently working on a search function in a wp site. The plan is to have a search function on the news page which just searches posts.

I'm achieving this by adding the below to my functions.php file:

   /* search just posts */
function SearchFilter($query) {
  if ($query->is_search) {
  $query->set('post_type', 'post');
return $query;


which works fine. BUT. I also need to create a search on a custom post type listing - for just the custom posts and also one for the entire site to live on the 404 page.

Any ideas how to go about this greatly appreciated?

share|improve this question
up vote 4 down vote accepted

you can change your search filter to this:

function SearchFilter($query) {
$post_type = $_GET['post_type'];
if (!$post_type) {
    $post_type = 'any';
if ($query->is_search) {
    $query->set('post_type', $post_type);
return $query;


then on your news search form add :

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

on your custom post type listing search form add

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

and on the entire site search for don't add anything an you are set.

share|improve this answer
So I use the value of the hidden field to deterine the post type? like: <input type="hidden" name="post_type" value="post" /> <input type="hidden" name="post_type" value="custom" /> Just wanted to clarify as your examples above are the same? Looks good though! :) Thanks Andy – tbwcf Feb 25 '11 at 12:07
@tbwcf that's correct, i'll update the code when i get back home – Bainternet Feb 25 '11 at 12:14
Looks perfect! Thank you very much! :) – tbwcf Feb 25 '11 at 12:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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