32

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

1
  • 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: frank-verhoeven.com/using-session-in-wordpress For me installing Peter Wooster's "Simple Session Support" plugin did the trick. There's a link to the plugin in the comments section of the post.
    – SteveR
    Sep 14, 2012 at 22:43

4 Answers 4

16

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.

5
  • Hi Brady, Thx for this example. Could I ask you, if possible, to share the form? In it, what URL are your submitting it to?
    – salocin
    Jun 28, 2012 at 7:46
  • @salocin - That information can be gotten by looking at the source of the page on the given URL in answer
    – Scott
    Jun 28, 2012 at 8:06
  • thx Brady, so url of the page template?
    – salocin
    Jun 28, 2012 at 11:49
  • The form is on catalysthomes.co.uk. You can view source and see how the form works
    – Scott
    Jun 28, 2012 at 13:15
  • Are you check to see if the custom taxonomies have posts with them as a custom field? How exactly are you populating the search form? @Brady thanks Feb 19, 2016 at 6:00
2

Try Taxonomy Picker plugin together with Relevanssi. Killer combination.

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

1
  • this doesn't take into account post relationships, that being the difficult part of my problem. It's way easier to filter posts by taxonomy, I need to filter them buy the taxonomy (or custom field) of a related post.
    – pax
    Jan 14, 2012 at 17:09
0

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

1
  • It doesn't suit the approach that I need (with specific filters), but it's a very interesting plugin, it looks like a great replacement for the default search function, thanks very much for pointing it out.
    – pax
    Jan 10, 2012 at 17:37
0

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.

1
  • 1
    Hi Steve. Thanks for posting your first answer here. Glad to have you at WPSE. For future reference, answers shouldn't pivot completely on an external link. If the link is disabled, your answer becomes benign. Would you mind updating your answer with a couple of sample relevant code snippets? Sep 16, 2012 at 4:09

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