Skip to content

Instantly share code, notes, and snippets.

@jhned
Last active August 29, 2015 14:12
Show Gist options
  • Save jhned/bbf51513c075666a3171 to your computer and use it in GitHub Desktop.
Save jhned/bbf51513c075666a3171 to your computer and use it in GitHub Desktop.
WP Custom Meta DropDowns: A dropdown function for custom meta that mirrors wp_dropdown_categories. To use it, set up a form that has a "get" method, pass in your custom meta data to check against, and then use an added query_var and pre_get_posts in order to pass the new meta query in to $wp_query.
<?php
add_filter( 'query_vars', 'custom_meta_dropdowns_query_args' );
function custom_meta_dropdowns_query_args($vars) {
$vars[] = 'locations';
$vars[] = 'area-of-practice';
return $vars;
}
add_action( 'pre_get_posts', 'set_physicians_args' );
function set_physicians_args($query) {
if ( $query->is_main_query() && $query->is_post_type_archive('physician') && !is_admin() ) {
$location = get_query_var('locations');
$practice_area = get_query_var('area-of-practice');
if ( $location != -1 || $practice_area != -1 ) {
$meta_query = $query->get('meta_query');
if ( $location != -1 && $location != '' ) {
$meta_query[] = array(
'key' => 'physician_location'
, 'value' => $location
);
}
if ( $practice_area != -1 && $practice_area != '' ) {
$meta_query[] = array(
'key' => 'physician_practice'
, 'value' => $practice_area
, 'compare' => 'LIKE' // If the item is in a serialized array, use LIKE.
);
}
if ( !empty($meta_query) ) {
$query->set( 'meta_query', $meta_query );
}
}
}
}
<?php
if ( is_post_type_archive('physician') ) {
$l_items = get_posts('post_type=location&numberposts=-1&orderby=menu_order&order=ASC');
$location_args = array(
'name' => 'locations'
, 'items' => $l_items
, 'show_option_all' => '-- All Locations --'
, 'selected' => get_query_var('locations')
);
$a_items = get_posts('post_type=area-of-practice&numberposts=-1&orderby=title&order=ASC');
$areas_args = array(
'name' => 'area-of-practice'
, 'items' => $a_items
, 'show_option_all' => '-- All Practice Areas --'
, 'selected' => get_query_var('area-of-practice')
);
?>
<div class="physicians-filter">
<h3 class="block-title">Filter Physicians</h3>
<form method="get" action="/physicians">
<?php custom_meta_dropdowns($location_args); ?>
<?php custom_meta_dropdowns($areas_args); ?>
<button type="submit">Filter</button>
</form>
</div>
<?php } ?>
function custom_meta_dropdowns($args) {
$defaults = array(
'name' => ''
, 'value' => 'ID'
, 'items' => array()
, 'echo' => true
, 'show_option_all' => ''
, 'selected' => ''
);
$vars = wp_parse_args($args, $defaults);
$select_o = '<select id="'. $vars['name'] .'" name="'. $vars['name'] .'">';
$select_c = '</select>';
if ( $vars['value'] == 'slug' ) {
$item_key = 'post_name';
}
else {
$item_key = 'ID';
}
foreach ($vars['items'] as $key => $item) {
if ( !is_object($item) ) {
continue; // We can build this in later to accept IDs as well as objects, if necessary.
}
$value = $item->$item_key;
$title = $item->post_title;
$selected = '';
if ( $vars['selected'] == $value ) {
$selected = 'selected="selected"';
}
$options[] = '<option value="'. $value .'" '. $selected .'>'. $title .'</option>';
}
if ( empty($options) ) {
return '<!-- No options available -->';
}
if ( $vars['show_option_all'] !== '' ) {
array_unshift($options, '<option value="-1">'. $vars['show_option_all'] .'</option>');
}
$output = $select_o . implode("", $options) . $select_c;
if ( $vars['echo'] == true ) {
echo $output;
}
else {
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment