Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer 1

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:

<?php
/**
 * Add a parameter for a custom field search
 */
add_filter('query_vars', 'wpse_35639_search_queryvars' );
function wpse_35639_search_queryvars( $qvars ) {
    $qvars[] = 'bedrooms';
    return $qvars;
}


/**
 * Intercept the posts query to add in our meta query if necessary
 */
add_action('pre_get_posts','wpse_35639_intercept_search');
function wpse_35639_intercept_search() {
    global $wp_query;

    if ( ! is_admin() && isset($wp_query->query_vars['bedrooms']) && is_numeric($wp_query->query_vars['bedrooms']) ) {
        # Limit the search to the floor_plan custom post type
        $wp_query->set('post_type', 'floor_plan');

        # This may seem unconventional, but we're setting that this is a search
        # even though WP doesn't recognize it as one. This is to leverage the search template
        $wp_query->is_search = true;

        # Set the meta query comparison
        $wp_query->set('meta_query', array(
            array(
                'key' => 'number_of_bedrooms',
                'value' => $wp_query->query_vars['bedrooms'],
                'compare' => '=',
                'type' => 'NUMERIC'
            )
        );
    }
}
?>

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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