Family Encyclopedia >> Electronics

How to Create Multiple Custom Search Forms in WordPress for Specific Post Types

As experienced WordPress developers, we've helped countless sites improve search functionality. Following our guide on limiting results to specific post types, here's how to build entirely separate search forms—each tailored to one post type—for a more intuitive user experience. This requires basic theme template knowledge.

Step 1: Add Search Forms to Your Site

Place these forms anywhere on your site, like sidebars or pages:

<form method="get" action="/">
  <input type="hidden" name="stype" value="normal" />
  <input type="text" name="s" />
  <input type="submit" value="Search Posts" />
</form>

For books (or any custom post type):

<form method="get" action="/">
  <input type="hidden" name="stype" value="books" />
  <input type="text" name="s" />
  <input type="submit" value="Search Books" />
</form>

Adjust the stype value as needed.

Step 2: Modify search.php

Backup your search.php first, then copy its loop code to clipboard. Replace the file contents with:

<?php
$stype = isset($_GET['stype']) ? $_GET['stype'] : 'normal';
include($stype . '-search.php');
?>

This directs searches to custom handlers.

Step 3: Build Handler Files

In your theme folder, create normal-search.php:

<?php
$args = array('post_type' => 'post');
$args = array_merge($args, $wp_query->query);
query_posts($args);
?>

Paste the original loop code right after. This searches posts only.

For books-search.php:

<?php
$args = array('post_type' => 'books');
$args = array_merge($args, $wp_query->query);
query_posts($args);
?>

Add the loop after. Repeat for more forms. Pro tip: For modern sites, prefer pre_get_posts hooks over query_posts to avoid deprecation issues.