Support » Plugins » Hacks » Global variable to keep term constant throughout theme?

  • I know this sounds convoluted, but I am trying to make a site for 3 dormitories on my campus. The way the information works is that we filtered information for each dorm using taxonomy terms (1 for each school) and adding them to the loops.

    Problem is we plan on adding more dorms, and each one has its own set of information that makes it hard to navigate.

    I wanted to know if there is a way I can make a front-page form to pick a specific taxonomy term:

    school 1, 2, 3

    And that variable can be included throughout the theme until someone picks another? So if I pick school 1, every page I go to throughout the site will filter all information and loops with content only with school 1’s taxonomy term.

    I was thinking adding a global in functions

    function school_global() {
        global $school;
        $school = $whatever_term_chosen_from_form

    Then in the loop I could put something like:

    $args = array(
    	'post_type' => 'events',
    	'school' => $school
    $query = new WP_Query( array( 'post_type' => 'events','taxonomy' => 'school','school' => $school,'posts_per_page' =>'3' ) );

    I would also use that variable to retrieve other things that will be named the same, such as link category lists and images.

    But my goal is trying to make a form that assigns a global variable that will be embedded into all loops until another variable is assigned. Any ideas?

    Solutions like breadcrumbs or multi-site isn’t helpful for many reasons I don’t want to get into, but if anyone can help I appreciate it!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    You have the right idea, but it’s more complicated. The global persists for the current request and no longer. Once a new page is requested, the whole process starts over. You will need to store the user selection in a cookie so each new page will know what the selection is. If all users must login, you could store their school choice in user meta instead. The advantage of having to do this is the choice will persist as long as you like, no matter how long before the user returns, their preference will still be there.

    There’s a couple approaches that come to mind to alter every query to filter by a particular taxonomy retrieved from the cookie or user meta. You could hook something like the action ‘parse_query’ and insert the additional query vars needed to do a taxonomy query. You’ll have to do some trial runs to determine just what query vars to add.

    Or maybe hook a filter like ‘posts_request’ and insert the necessary SQL to limit the results to your taxonomy. Which one to choose depends on whether you’re more comfortable hacking SQL queries or undocumented array structures.

    BTW, that’s not quite how to query by taxonomy. See Class_Reference/WP_Query#Taxonomy_Parameters. I know you were just illustrating a concept:)

Viewing 1 replies (of 1 total)
  • The topic ‘Global variable to keep term constant throughout theme?’ is closed to new replies.