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!
The session suggestion was the key. I opted for a cookie instead. So, first I added a function that checks for the cookie on init of wordpress. If it does not exist, we try to determine their location and write the cookie. Then in the pre_get_post hook we direct them to either the page with the city deals or ff their city does not match an existing city, we direct them to the search city page:
So now we have them in the search city page. We pull the available cities from an attribute field and list them, upon click a javascript in the header is executed to update the cookie and send them to the deals:
Here is the javascript, we delete the existing cookie and set the new one:
Works like a charm! Thank Halfer for the session suggestion.