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.

I'm trying to add drop down filters (that are populated by terms that are in use) to a custom search page for "opportunities" or listings page. Currently I am doing this with taxonomies but doing this with meta data, meta_key, meta_query is new to me.

I've searched around and most questions are not really dealing with pre-determined filters like this.

I just found out about http://wordpress.org/extend/plugins/facetious and http://wordpress.org/extend/plugins/advance-wp-query-search-filter from this post: advanced wordpress search with drop down menus but a plugin may be overkill since I've gotten the other parts of this working.

The general idea is grab all the values of "op_duration_meta" (e.g. "one-time", "short-term", "long-term") and turn it into a drop down that will sort the list or in combination with the other drop downs.

I was starting to think I was going to have to resort to appending stuff to the URL or something manually.

Thanks for any help in advance.

<div class="search">

    <form method="get" class="search-form" action="<?php echo home_url(); ?>">
    <div>
        <?php $query_types = get_query_var('post_type'); ?>

        <?php $args15 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'neighborhoods',
            'pad_counts'               => false );?>

        <?php $args16 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'post_tags',
            'pad_counts'               => false );?>


        <?php $args17 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'member_agencies',
            'pad_counts'               => false );?>

        <?php $args18 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false );?>

        <?php $args19 = array(
            'meta_key'                 =>'op_duration_meta',
            'post_status'              =>'publish',
            'post_type'                =>'opportunities',
            'orderby'                  =>'date',
            'order'                    =>'DESC' );
            ?>

        <div style="min-height:50px;display:block;"><label for="neighborhoods">1. Choose Neighborhood:</label><br/>
            <select name="neighborhoods">
             <option value=""><?php echo esc_attr(__('Select Neighborhood...')); ?></option>

        <?php $taxx1 = get_categories($args15);
        foreach ($taxx1 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>
        <div style="min-height:50px;display:block;">
        <label for="member_agencies">2. OR Choose Member Agency:</label><br/>
        <select name="member_agencies">
         <option value=""><?php echo esc_attr(__('Select Member Agency...')); ?></option>
         <?php $taxx2 = get_categories($args17); foreach ($taxx2 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>
        <div style="min-height:50px;display:block;">
        <label for="category">3. Choose Category:</label><br/>
        <select name="category">
         <option value=""><?php echo esc_attr(__('Select Category...')); ?></option>
         <?php $taxx3 = get_categories($args18); foreach ($taxx3 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>

        <div style="min-height:50px;display:block;">
        <label for="duration">3. Choose Duration:</label><br/>
        <select name="duration">
         <option value=""><?php echo esc_attr(__('Select Duration...')); ?></option>
         <?php

              $key_1_value = get_post_meta($args19);

              foreach ($key_1_value as $category) {
                // check if the custom field has a value
                $option = '<option value="Opportunity Duration">';
                $option .= $category;
                $option .= '</option>';
                echo $option;
            }
             ?>
        </select>
        </div>

        <div><input type="hidden" name="post_type" value="opportunities" checked="checked"/>

        <input style="height:24px;display:inline-block" class="search-submit button" name="submit" type="submit" value="<?php esc_attr_e( 'Sort This List', 'prototype' ); ?>" /></div>
    </div>
    </form><!-- .search-form -->

</div><!-- .search -->
share|improve this question

1 Answer 1

Because you want the results of the filtering process be the main object of your page, you may need to alter the main query using pre_get_posts action hook. It is really better that leave the main query run, load your theme and then say to WordPress: go back and run another query as suggested in other answers.

//Functions for filters
add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){

     //limit to main query, frontend and archive pages. You need another set of checkings, like check if the request is for your custom post type
     if($query->is_main_query() && !is_admin() && $query->is_archive ) {

          $meta_query = array();
          $meta_input = isset($_POST['meta_input']) ? $_POST['meta_input'] : '';

          if(!empty($meta_input)){
              $meta_query[] = array(
                  'key'  => 'your_meta_key',
                  //sanitize $_POST['meta_input'] according with your data type
                  'value'    => $_POST['meta_input'],
              );
          } 
          $query->set('meta_query',$meta_query);
    }

}

See more information about the meta_query.

share|improve this answer

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.