Id like to build a search form for a specific custom post type (vehicle) and have filters for that custom posts type's custom meta fields (price, age) and custom taxonomies (make). This will totally replace the sites search and be the only search available so I was going to use search.php within my custom template.

I've like the search to look something like:

Search our vehicles

Make (select box full of all the custom taxonomies - Audi, BMW etc).

Model (normal input field for people to type whatever in).

Prices over (select box with prices starting at 1000)

Age (select box with options such as under 1 year, under 3 years, under 5 years, under 10 years).

I'm new to custom fields and dont really know where to start (I've found a few examples on Google but none do exactly what I'm aiming for). I didnt really want to use a plugin either. I'm guessing within search.php I grab the data passed from the form and use it to build $args up to pass to WP_Query?

Could someone point me in the right direction please? Thanks in advance

  • Have you seen this answer?
    – kaiser
    Commented Jul 8, 2013 at 13:04
  • I'd not but it seems massively complex for what I'm trying to do?
    – Jimothey
    Commented Jul 8, 2013 at 13:06
  • 1
    "massively complex" means you don't want read that much text, right? :P Please read it. It will bring you down half the way minimum.
    – kaiser
    Commented Jul 8, 2013 at 13:08
  • Any progress on that question?
    – kaiser
    Commented Aug 1, 2013 at 0:01

2 Answers 2


If you want to extend your query, you should extend it through the pre_get_posts-filter. Then just do a "Custom Field" or meta query.

add_action( 'pre_get_posts', 'wpse105969_extended_search' );
function wpse105969_extended_search( $query )
    // Make sure we got a search query
    // and we're only modifying the main query.
    if (
        ! $query->is_main_query()
        AND '' === $query->get( 's' )
        AND 'your_custom_post_type' === $query->get( 'post_type' )
        return $query;

    // Alter whatever you need: Make, Model, etc.
    $query->set( 'meta_query', array(
        'relation' => 'OR',
            'key'     => 'color',
            'value'   => 'blue',
            'compare' => 'NOT LIKE'
            'key'     => 'price',
            'value'   => array( 20, 100 ),
            'type'    => 'numeric',
            'compare' => 'BETWEEN'
    ) );

    return $query;
  • Hi thanks for the answer - I dont actually use the 's' field that normal wordpress searches require (im only search for the custom post types meta details). Is there anyway to NOT use the 's' field in the search form, struggling to find info on this. Thanks again.
    – Jimothey
    Commented Jul 8, 2013 at 19:27
  • Also, I've added your code to my functions.php and added in my fields but when i save it breaks wordpress (no errors). Nothing from wordpress works (nav, posts etc).
    – Jimothey
    Commented Jul 8, 2013 at 19:45
  • @JamesJ Please post an update with your code. Hint: Did you change the custom post type name and did you set WP_DEBUG to true?
    – kaiser
    Commented Jul 9, 2013 at 21:21

Here’s the code. You can change $post_type and $custom_fields according to your needs.

function extend_admin_search( $query ) {

    // Extend search for document post type
    $post_type = 'document';
    // Custom fields to search for
    $custom_fields = array(

    if( ! is_admin() )

    if ( $query->query['post_type'] != $post_type )

    $search_term = $query->query_vars['s'];

    // Set to empty, otherwise it won't find anything
    $query->query_vars['s'] = '';

    if ( $search_term != '' ) {
        $meta_query = array( 'relation' => 'OR' );

        foreach( $custom_fields as $custom_field ) {
            array_push( $meta_query, array(
                'key' => $custom_field,
                'value' => $search_term,
                'compare' => 'LIKE'

        $query->set( 'meta_query', $meta_query );

add_action( 'pre_get_posts', 'extend_admin_search' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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