Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.