DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Create an Advanced Search Form for Custom Post Types in WordPress

Use a GET form and validated WP_Query arguments to search a WordPress custom post type by keyword, taxonomy, and custom fields.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To search a WordPress custom post type and let visitors narrow results, build a GET form and pass its validated values into a WP_Query. Set post_type explicitly; use s for keywords, tax_query for taxonomy terms, and meta_query for custom fields. For a few fixed filters, custom code is often straightforward. For many facets or editor-managed controls, a plugin may be a better fit.

Choose how to build the search

A small custom form and query work well when you know which fields visitors need and can maintain the code. A plugin is worth considering when filters are numerous, need richer controls, or should be managed by editors.

As an Amazon Associate I earn from qualifying purchases.

  • Custom code: You control the form, validation, query, and results template. Someone must maintain those pieces as the site changes.
  • Plugin: A plugin can expose configurable filters, but compatibility depends on the site’s theme, listing query, and setup. Check current features, maintenance, licensing, and price before choosing.

Build a GET form for a results page

Use a stable page or archive as the form’s destination. GET submissions put filter selections in the URL, making searches easier to bookmark and share. Keep parameter names predictable and decide which filters visitors actually need.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, a form might submit a keyword as q and a genre selection as genre. The names should correspond to the parameters your handler reads; they do not need to match WordPress query argument names.

Read and validate submitted filters

Do not let visitor input choose an arbitrary post type, metadata key, or query structure. Set those in server-side code. Sanitize text, and validate selections against the registered taxonomy and its terms before using them. For numeric filters, use a numeric validator and enforce an allowed range.

This illustrative handler searches published items in a post type called book, optionally matching a keyword and a genre term:

<?php
$search = isset( $_GET['q'] )
    ? sanitize_text_field( wp_unslash( $_GET['q'] ) )
    : '';
$genre = isset( $_GET['genre'] )
    ? sanitize_key( wp_unslash( $_GET['genre'] ) )
    : '';

$args = array(
    'post_type'      => 'book', // Replace with the registered post type slug.
    'post_status'    => 'publish',
    'posts_per_page' => 10,
);

if ( '' !== $search ) {
    $args['s'] = $search;
}

if ( '' !== $genre ) {
    // Validate that this slug belongs to the registered taxonomy before querying.
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'genre', // Replace with the registered taxonomy slug.
            'field'    => 'slug',
            'terms'    => $genre,
        ),
    );
}

$query = new WP_Query( $args );

Replace the example slugs with the site’s registered values. The example is a teaching pattern, not a tested drop-in plugin; term validation is an essential part of adapting it. WordPress documents the relevant WP_Query arguments and structures.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Add taxonomy and custom-field filters deliberately

Use tax_query when filtering by terms in a taxonomy, and meta_query for custom-field values. Both use nested arrays. When there are multiple clauses, choose whether all conditions must match (AND) or any may match (OR); do not leave the relationship implicit in your design. Build clauses only for validated, non-empty selections.

Before adding a metadata filter, establish the field’s data type and stored value format. For numeric ranges, use numeric comparison settings so values are compared as numbers rather than strings. Keep supported metadata keys and comparison options fixed in server-side configuration.

Render results safely and preserve filters

Render the loop using the site’s existing template conventions. Escape values for the output context, including attributes and visible content. WordPress’s get_search_query() returns the search query escaped for an HTML attribute by default, and esc_attr() is available for attribute values. Apply the appropriate escaping to selected options, input values, and result content.

If you add pagination, carry the active filter values into the pagination links and use the correct page variable for the page context. Test that the results and selected controls remain consistent as visitors move between pages.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a custom query or modify the main query?

A dedicated results template can run its own WP_Query, giving you direct control over the post type and filters. If you instead modify the global main query, scope changes carefully with pre_get_posts so the intended front-end query is affected and other queries are not. Registering a custom post type alone does not add it to the main query, as the WordPress Plugin Handbook explains.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When a plugin may be a better fit

Search & Filter describes filtering by post type, custom taxonomy, post date, custom fields, and other dimensions. Its listing identifies AJAX results and additional custom-field controls as Pro features. Confirm the current edition, compatibility, support, and licensing details before relying on a specific capability.

FacetWP documentation covers taxonomy facets, and its custom post type guidance says searchable custom post types can be filtered. FacetWP filters a FacetWP-enabled listing query, so the site’s query and template integration matter.

Compare options against the actual site: how many filters are needed, whether editors must change them without developer help, who will maintain the setup, whether the theme or page builder supports the results query, and whether page reloads are sufficient or richer interactions are needed. Verify current pricing and licensing directly before purchase.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Expose search through the REST API when needed

For a JavaScript-driven interface, a custom post type can use WordPress’s default REST collection routes when it is registered with show_in_rest => true. By default, these routes use the wp/v2 namespace. If the default collection behavior cannot express the required filters, use a custom route or controller. See the WordPress REST API documentation for custom content types.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.