On my custom post type pages I have custom fields that the user can edit.
What I want is to have a search form with 2 select dropdowns: one dropdown that is populated with all the top-level custom post type pages, and the other dropdown to narrow the results down to show pages that have a certain custom field value.
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>
I’m really having trouble figuring this out, if someone could help it would be greatly appreciated.
Looks like this question is a several months old, but it’s a good one so I’m digging it out of the grave.
The way I would go about solving it would be to intercept any searches with the
pre_get_posts
filter and add in the meta query based on the provided information. Here’s a basic shot at the solution, which can become a plugin or go in your theme’s functions.php file:Note that this just solves the custom field portion of the search. Searching by page is much easier (and I gather you solved that already).