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

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;
}

Leave a Comment