Custom search for custom post type, custom meta and search fields

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:

Read More

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

Related posts

2 comments

  1. 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',
            array(
                'key'     => 'color',
                'value'   => 'blue',
                'compare' => 'NOT LIKE'
            ),
            array(
                'key'     => 'price',
                'value'   => array( 20, 100 ),
                'type'    => 'numeric',
                'compare' => 'BETWEEN'
            )
        ) );
    
        return $query;
    }
    
  2. 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(
            "_file_name",
        );
    
        if( ! is_admin() )
            return;
    
        if ( $query->query['post_type'] != $post_type )
            return;
    
        $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' );
    

Comments are closed.