This comes up often enough so I thought I should make a quick post about it. The deal is that you want to pass a variable in the URL string to a WordPress page so you can customize the page based on it.

For example you have a list of events on page with the URL of /events but you want to be able to pass a filter to it so it only shows future or only past events like so:

/events?show=future or /events?show=past

Usually these are the scenarios but you can think up dozens of other scenarios I bet. Like tracking ?id=G5f47id’s etc.

WordPress pass query variables in URL

WordPress will ignore any query variables passed to a page unless it was specifically registered. In the above example the ?show=variable would be completely ignored unless registered. Here are the steps to be able to use these.

  • Register it in your function.php file
  • Read it out in you page template file

Register the variable

Register the query variable in your functions.phpfile of your child theme by hooking into query_vars. Here is the sample code.

<?php 
/**
 * Allow custom Query variable show
 * ex: www.domain-name.com/?show=future
 * change PREFIX_ to something meaningful like your initials ex: alm_
 */

function PREFIX_add_query_vars($aVars) {
	$aVars[] = "show"; 
	
	return $aVars;
  }
	
// hook PREFIX_add_query_vars function into query_vars
add_filter('query_vars', 'PREFIX_add_query_vars');
  

After doing this WordPress knows about your additional query variable. Its time to use it.

Read the variable

Before we continue, here is something you should note: NEVER use the query variable directly, always have a new variable set up for your use that is either sanitized or set up with if statements. Here is what I mean by it. In the variable that was passed in the URL you could have malicious Scripts or Strings, that could harm your DB if used directly. Look at the following example:

<?php 
    //----------------------------------------------
    // READ the 'show' variable
    //----------------------------------------------

    if (!empty( get_query_var('show'))){
        $q_show = get_query_var('show');
        switch ($q_show) {
            case 'future':
                echo "do filter on my future";
                $my_filter_variable = 'future';
                # code...
                break;
            case 'past':
                echo "do filter on my past";
                $my_filter_variable = 'past';
                # code...
                break;
            
            default:
                echo "it's giberish! Nice try Hacker!";
                # code...
                break;
        }
    }

    // do something with  $my_filter_variable
    // NOT with $q_show


    //----------------------------------------------
    // PROPER EXAMPLE CODE FOLLOWS NOT FOR REAL USE
    // Just for fun, here is query
    //----------------------------------------------

    // $today = time();

    // $events_compare_string = ">=";
    // if ( $my_filter_variable == 'past' ){
    //   $events_compare_string = "<";  
    // } else{
    //   $events_compare_string = ">=";
    // }

    // $args = array(
        // 'post_type'      => 'event',
        // 'meta_query' => array(
        //   array(
            // 'key' => 'event_date_time',
            // 'compare' => $events_compare_string, 
            // 'value' => $today,
            // 'type' => 'number',
        //   )
        // ),
        // 'meta_key'  => 'uscprice-event_date_time',
        // 'order'     => 'ASC',
            // 'posts_per_page' => -1,
            // 'post_status'    => 'publish',
        // 'paged'          => get_query_var( 'paged' ),
    //   );

    //   $wp_query = new WP_Query( $args );

Note that whatever I read from the ?show=variable I just passed it to a Switch Case or and assigned a new variable $my_filter_variable a value from fixed strings that is totally separate from what I read. Now Based on this new variable I can do whatever I please since I know what can be in it.

Then just for fun after the comment // PROPER EXAMPLE CODE FOLLOWS NOT FOR REAL USE I added a query for you to see how would I list Past or future events.

In this example my query variable could only be past or future or not defined. In some cases you might need to use the string from the URL, in that case be sure to use the proper sanitization before passing it on.

Cheers! I hope this helped.