I have in my themes some code which will set a session variable. For the sake of example, it's a geolocation routine which will set to either USA, UK or ROW, resulting in:

$_SESSION['region'] = 'USA';

and I have a custom taxonomy of 'regionok' which any or all of the same terms are set.

I wish to use the session variable value as the taxonomy term to filter on.

The cleanest solution I can imagine would be to have a filter hook in my functions.php which would replace the placeholder term 'auto-region' with the session value before each query is executed. That way I can use widget plugins without having to hack them - I would just use the placeholder term in configuration. However, I don't know what the relevant hook would be.

(Other use cases would be session variables for family filter, referrer, etc. The geolocation example is entirely incidental.)


If you want to apply this term filter to all queries on the site, then you'll want to use the 'parse_query' action. This action will accept the WP_Query instance as a parameter, which will allow you to add a term filter based on the $_SESSION variable.

  • Yep. That works. Ta. – Paul Gregory Jan 10 '12 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.