WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to do two things:

  1. Add a CGI variable (say filter=21) to the current query and make a link to this modified URL.
  2. Fetch the value of CGI variable "filter".

How to do that in WordPress?

share|improve this question
up vote 2 down vote accepted

First off, these aren't CGI variables, they're query string arguments.

WordPress, by default, will remove any query arguments that it doesn't recognize. So you need to register them with WordPress and then pull them back out of the query.

First, add your new query variable:

function wpa_48528_vars( $vars ) {
    $vars[] = 'filter';

    return $vars;
add_filter( 'query_vars', 'wpa_48528_vars' );

Now, get the data back out:

function wpa_48528_get_filter() {
    global $wp_query;

    if ( array_key_exists( 'filter', $wp_query->query_vars ) ) {
        return $wp_query->query_vars['filter'];

    return '';
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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