Family Encyclopedia >> Electronics

How to Build an Advanced Search Form in WordPress for Custom Post Types: Expert Tutorial

As experienced WordPress developers, we've optimized sites with diverse custom post types—resource articles, videos, books, and blog posts. Default WordPress search lacks precision, and Google Custom Search isn't always ideal. For a recent project, we engineered an advanced form with checkboxes for post type selection, persistent queries, and easy refinements using native WordPress search.

Follow this proven step-by-step guide to implement it yourself.

Start by editing your searchform.php (or custom search form file). Add these hidden fields inside the <form> tag:

<input type="hidden" name="post_type[]" value="articulos" />
<input type="hidden" name="post_type[]" value="blog" />
<input type="hidden" name="post_type[]" value="libros" />
<input type="hidden" name="post_type[]" value="videos" />

Replace values with your post types—we exclude 'page' to prioritize content. These ensure default broad searches and checkbox state persistence via in_array checks.

Next, in search.php, add this refinement form above the loop:

<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
  <input type="search" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" />
  <br />
  Artículos <input type="checkbox" name="post_type[]" value="articulos" <?php checked( in_array( 'articulos', $_GET['post_type'] ?? [] ) ); ?> />
  Blog <input type="checkbox" name="post_type[]" value="blog" <?php checked( in_array( 'blog', $_GET['post_type'] ?? [] ) ); ?> />
  Libros <input type="checkbox" name="post_type[]" value="libros" <?php checked( in_array( 'libros', $_GET['post_type'] ?? [] ) ); ?> />
  Videos <input type="checkbox" name="post_type[]" value="videos" <?php checked( in_array( 'videos', $_GET['post_type'] ?? [] ) ); ?> />
  <input type="submit" value="Search" />
</form>

It prefills the query and checks active post types. Modify and resubmit with ease.

Preview of the form in action:

How to Build an Advanced Search Form in WordPress for Custom Post Types: Expert Tutorial

Users can tweak parameters effortlessly.

This approach stems from our real-world implementations and WordPress forum insights—shoutout to @tammyhart for the query check technique. Many guides fall short; we hope this empowers you.