09/10/2013 at 3:59 pm
#268911
Guest
Front-end code for your benefit.
##Unfortunately connections does not currently allow us (as far as I know) to get a list of all the categories, and we need them. So we're going to fetch them manually. This is likely to break at some point when the plugin's database gets changed in an update.
//set up wpdb class
global $wpdb;
//get parent-child relationships
$sql = "SELECT <code>term_id</code>,<code>parent</code> FROM <code>".$wpdb->prefix."connections_term_taxonomy</code>";
$taxonomy = $wpdb->get_results($sql);
//grab names
$sql = "SELECT * FROM <code>".$wpdb->prefix."connections_terms</code>";
$names_raw = $wpdb->get_results($sql);
//organize this a little better
foreach($names_raw as $name){
$names[$name->term_id]['name'] = htmlentities($name->name,ENT_QUOTES);
$names[$name->term_id]['slug'] = htmlentities($name->slug,ENT_QUOTES);
}
//sort through the taxonomies, grab their names, and build a list
$tier = array();
foreach($taxonomy as $tax){
//make sure we have a name for the parent (otherwise it's root)
if(!empty($names[$tax->parent]['slug']) && isset($names[$tax->parent]['slug'])){
$tier[$tax->parent]['children'][$tax->term_id] = $names[$tax->term_id];
}else{
$tier[$tax->term_id] = $names[$tax->term_id];
}
}
//now that we're all organized, let's make a dropdown!
unset($tier[1]); //remove uncategorized
$tier = array_filter($tier);
echo '<form method="get">';
echo '<select name="state" onchange="this.form.submit()">';
echo '<option value="0">Select...</option>';
//create our options (state level)
foreach($tier as $category_id => $state_data){
$selected = '';
if(intval($_GET['state'])===$category_id)$selected = 'selected="selected"';
echo '<option value="'.$category_id.'" '.$selected.'>'.$state_data['name'].'</option>';
}
echo '</select>';
//if we know the state, get counties
if(isset($_GET['state']) && !empty($_GET['state']) && is_numeric($_GET['state'])){
echo '<select name="county" onchange="this.form.submit()">';
echo '<option value="0">Select...</option>';
foreach($tier[$_GET['state']]['children'] as $id => $county){
$selected = '';
if(intval($_GET['county'])===$id)$selected = 'selected="selected"';
echo '<option value="'.$id.'" '.$selected.'>'.$county['name'].'</option>';
}
echo "</select>";
}
echo '</form>';
if(isset($_GET['county']) && !empty($_GET['county'])){
echo do_shortcode('[connections category="'.intval($_GET['county']).'"]');
}
