Advanced search form with filters for custom taxonomies and custom fields

I’d like to build an advanced search form for a specific custom post type, having filters for the custom post types custom fields, custom taxonomies, and for a separate custom post types properties (fields and taxonomies) which will be links to the first post type using a custom relationship field.

I’ve recently started off with WPs custom post types, fields, and taxonomies, I love it so far but to make the best of it I’d like to be able to search it properly. Do I need to do it manually? If so, how?

PS. If it matters, I’m using the plugins: Advanced Custom Fields and Custom Post Type UI.

Below I’ve mocked up an example of how the filtering would look and how it might relate to the post types above.

wordpress custom search form filtered by custom fields and custom taxonomies

Solutions Collecting From Web of "Advanced search form with filters for custom taxonomies and custom fields"

I think something like this you would be best writing yourself.

Take a look at: http://www.catalysthomes.co.uk/homes-for-sale/

Properties are loaded into a CPT and I have my own custom search in the sidebar. Of that search its searching a number of things such as taxonomies, custom fields and ordering by date price etc.

So how do I achieve this? I submit the form to a page template and from there I deal with the post data and build a new WP_query based on the search criteria. I use sessions to store the search variables so that I can paginate the results.

WP_Query is very powerful. Take a look: http://codex.wordpress.org/Class_Reference/WP_Query

In there you can use meta_query to query multiple custom fields and use tax_query to query your taxonomies, plus there is more. Below is how mine is built to give you an idea.

Template File:

  <?php
  $temp = $wp_query;
  $wp_query = NULL;
  $args = array();
  ?>

  <?php include("functions/Homes-for-sale/propertyrawresults.php"); ?>
  <?php include("functions/Homes-for-sale/propertysearchresults.php"); ?>

  <?php
  $args['post_type'] = "homes-for-sale";
  $args['showposts'] = 10;
  $args['paged'] = $paged;
  $wp_query = new WP_Query($args);
  ?>

  <?php include("functions/Homes-for-sale/propertylistlayout.php"); ?>

Raw Results

<?php
if($_POST['sortby']) {
    $_SESSION['prop_selectedsortby'] = $_POST['sortby'];
}

switch($_SESSION['prop_selectedsortby']) {
    case "name-asc": $args['order'] = "ASC"; $args['orderby'] = "title"; break;
    case "name-desc": $args['orderby'] = "title"; break;
    case "price-asc": $args['order'] = "ASC"; $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break;
    case "price-desc": $args['orderby'] = "meta_value_num"; $args['meta_key'] = "chb_homes_for_sale_specifics_fmv"; break;
    case "date-asc": $args['order'] = "ASC"; break;
    default: /* No need to set arguments here as wp query defaults */ break;
}

$selectedsortby[$_SESSION['prop_selectedsortby']] = " selected=\"selected\"";
?>

Search Results

<?php
if( ! empty( $_SESSION['s_property_ptype'] ) ) {
    $args['meta_query'][] = array(
        'key' => 'chb_homes_for_sale_types_nbrs',
        'value' => $_SESSION['s_property_ptype']
    );
}

if( ! empty( $_SESSION['s_property_development'] ) ) {
    $args['meta_query'][] = array(
        'key' => 'chb_homes_for_sale_ofdevelopment',
        'value' => $_SESSION['s_property_development']
    );
}

if( isset( $_SESSION['s_property_area'] ) && 0 != $_SESSION['s_property_area'] ) {
    $args['tax_query'][] = array(
        'taxonomy' => 'areas',
        'field' => 'id',
        'terms' => array( (int) $_SESSION['s_property_area'] ),
    );
}

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_bedrooms',
    'value' => $_SESSION['s_property_bedrooms_min'],
    'compare' => '>=',
    'type' => 'SIGNED'
);

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_bedrooms',
    'value' => $_SESSION['s_property_bedrooms_max'],
    'compare' => '<=',
    'type' => 'SIGNED'
);

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_bathrooms',
    'value' => $_SESSION['s_property_bathrooms_min'],
    'compare' => '>=',
    'type' => 'SIGNED'
);

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_bathrooms',
    'value' => $_SESSION['s_property_bathrooms_max'],
    'compare' => '<=',
    'type' => 'SIGNED'
);

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_fmv',
    'value' => $_SESSION['s_property_min_price'],
    'compare' => '>=',
    'type' => 'SIGNED'
);

$args['meta_query'][] = array(
    'key' => 'chb_homes_for_sale_specifics_fmv',
    'value' => $_SESSION['s_property_max_price'],
    'compare' => '<=',
    'type' => 'SIGNED'
);
?>

List Layout
Just a standard WP loop to show post excerpts and info.

Try Taxonomy Picker plugin together with Relevanssi. Killer combination.

http://www.squidoo.com/taxonomy-picker-wordpress-plugin
http://wordpress.org/extend/plugins/relevanssi/

Take a look at Relevanssi plugin, it might do what you are looking for: http://wordpress.org/extend/plugins/relevanssi/

If anyone is having difficulty implementing Brady’s solution above (as I did) here’s a hint: It appears that WordPress has some problems with passing session data so you will probably have to do something extra to make it work properly. The issues are discussed here

In functions.php:

function init_sessions() {
  if (!session_id()) {
  session_start();
 }
}
add_action('init', 'init_sessions');

In your template:

/**
* Enable sessions
*/
if (!session_id())
session_start();

For me installing Peter Wooster’s “Simple Session Support” plugin did the trick.