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: https://wordpress.stackexchange.com/questions/99119/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 -->
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.See more information about the meta_query.