I have a site that detects a user's location and only displays posts that have the taxonomy with the matching city. If there is no match the user is redirected to a page to select available cities. This is my function:
function my_location($q){
if (!$q->is_main_query())
return;
if ($q->is_search())
return;
if ($q->is_archive()){
if ( ! is_admin()) {
if ($userSlug!='Set'){
$userInfo = geoip_detect_get_info_from_current_ip();
switch ($userInfo->postal_code){
case '86403':
case '86404':
case '86405':
case '86406':
$city="lake-havasu-city";
break;
case '86401':
case '86402':
case '86409':
$city="kingman";
break;
case '86429':
case '86430':
case '86439':
case '86442':
$city="bullhead-city";
break;
default:
force_post_city($city);
exit;
}
$q->set( 'tax_query', array(array('taxonomy' => 'pa_city','field' => 'slug',terms' => array( $city ),'operator' => 'IN')));
}}
}
}
add_action( 'pre_get_posts', 'my_location' );
My question is, on the page that the user selects the city, how do I pass the city back to this function so they will pull the appropriate city? This is my form:
<form method="post" action="new_location($term_taxonomy)">
<?php
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
$optionname = "optionname";
$emptyvalue = "";
$output ="<select name='".$optionname."'><option selected='".$selected."' value='".$emptyvalue."'>Select a City</option>'";
foreach($myterms as $term){
$term_taxonomy=$term->pa_city; //CHANGE ME
$term_slug=$term->slug;
$term_name =$term->name;
$link = $term_slug;
$output .="<option name='".$link."' value='".$link."'>".$term_name."</option>";
}
$output .="</select>";
return $output;
}
$taxonomies = array('pa_city'); // CHANGE ME
$args = array('order'=>'ASC','hide_empty'=>true);
echo get_terms_dropdown($taxonomies, $args);
?>
<input type="submit" value="click" name="submit">
</form>
Any help would be much appreciated!
actiontarget for your city form? If so, when the user submits, could you set a session variable? Thus, when the site tries to show its posts by taxonomy, it can try to detect the city, first via geo/IP detection, then by session. In fact I'd cache the geo/IP detection in the session too - you don't want to be making a redundant HTTP call for every page view.