Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help with Custom Search - Custom Post Type + Custom Field WordPress

  • SOLVED

I could really use help with this problem. Here's the situation:
My site has custom post type pages, and in these pages I have custom fields that the user can edit.
What I want is to have a search form to let visitors easily narrow down their search with 2 select dropdowns: one dropdown that is populated with all the top-level custom post type pages (building type), and the other dropdown to narrow the results down to show pages that have a certain custom field value (number of bedrooms).

So far I have not been able to get the results to filter via the custom field. Here is my form code so far:
<form method="get" action="<?php echo get_permalink($properties_search_id); ?>">
<input type="hidden" name="post_type" value="floor_plan" />
<ul class="wpp_search_elements">
<li class="wpp_search_group wpp_group_not_a_group">
<ul class="wpp_search_group wpp_group_not_a_group">
<li>
<label class="wpp_search_label wpp_search_label_bedrooms" for="wpp_search_element_7165">Bedrooms<span class="wpp_search_post_label_colon">:</span></label>
<div class="wpp_search_attribute_wrap">
<select name="bedrooms" class="bedrooms">
<?php
$metakey = 'number_of_bedrooms';
$bedrooms = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
if ($bedrooms) {
foreach ($bedrooms as $bedroom) {
echo "<option value=\"" . $bedroom . "\">" . $bedroom . "</option>";
}
}
?>
</select>
</div>
<div class="clear"></div>
</li>
<li>
<label class="wpp_search_label wpp_search_label_property_type" for="wpp_search_element_7437">Property Type<span class="wpp_search_post_label_colon">:</span></label>
<div class="wpp_search_attribute_wrap">

<?php
$args = array(
'child_of' => 0,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 2,
'depth' => 1,
'post_type' => 'floor_plan'
);
wp_dropdown_pages( $args );
?>

</div>
<div class="clear"></div>
</li>
</ul>
<div class="clear"></div>
</li>
<li class="wpp_search_form_element submit">
<input type="submit" value="Search" class="wpp_search_button submit">
</li>
</ul>
</form>


You can see this code in action at http://ames.mvmdata.com
I could really use some guidance asap.

Thanks!

Answers (1)

2011-12-05

Luis Abarca answers:

Its something like this filters http://mchome.com.mx/propiedades/, i can help you, let me prepare the code ;)

I recommend you to use custom taxonomies instead.


Luis Abarca comments:



add_action('pre_get_posts', 'realty_dosearch');

/**
*
* Ejecuta la busqueda de propiedades
*/
function realty_dosearch()
{
global $wp_query;

$bedrooms = get_query_var('bedrooms');

// Area
if (!empty($area) && $area > 0) {
$meta_query = array();

$meta_query[] = array(
'key' => 'bedrooms',
'value' => $bedrooms,
'compare' => '=',
'type' => 'NUMERIC'
);

$wp_query->query_vars['meta_query'] = $meta_query;
}
}

// }}}
// {{{

add_filter('query_vars', 'realty_query_vars');

function realty_query_vars($query_vars)
{
$query_vars[] = 'bedrooms';
return $query_vars;
}



Nathan Parikh comments:

thanks, how would I integrate this with the form with the 2 dropdowns?


Nathan Parikh comments:

I need it to integrate the bedroom custom field and only search the custom post type floor_plan


Luis Abarca comments:

Upps, it was 'number_of_bedrooms' instead of 'bedrooms'


Luis Abarca comments:

I updated the code on pastebin

http://pastebin.com/i4zEHyRw


Luis Abarca comments:

If you are using the dropdown with the permalink you dont need to use a filter for post type, you actually are sending the search to a specific custom post type single page (floor plan).

You should change the custom post type for a custom taxonomy and then we can filter with tax_query.


Nathan Parikh comments:

how do you suggest I add the custom taxonomy? I need to keep the custom post type feature, but if I need to add a custom taxonomy that's fine...just let me know how :)


Luis Abarca comments:

I think, floor plan and # of beedrooms should be custom taxonomies.

Then you can select # of bedrooms and the building for your custom post type.

Its what i recommend you.

I can help you more if you can send me login details.


Nathan Parikh comments:

Thank you very much Luis, you've been great! You saved me a lot of trouble! :)