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 wish to create a new variable as described in the codex, but on submission of the URL the variables are submitted twice to my function

I have tried this within a plugin also a the function.php. I have also tried to use wp_reset_query()

Here is my sample code:

add_filter('query_vars', 'my_function');
function my_function ($vars) {    
  $vars[] = 'Q1';
  $vars[] = 'Q2';    
  return $vars;    
}

add_action('parse_query', 'query_var');    
function query_var () {    
  if (get_query_var('Q1')){    
    echo ('hello');    
  }
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

The parse_query action gets called on every query (menu items, sidebar recent posts widgets, etc.), but get_query_var pulls from the main query, so that condition will be true for all queries despite the fact that the query var doesn't exist in those queries. You need to check the query object passed to your function hooked to parse_query:

add_action('parse_query', 'query_var'); 
function query_var( $query ) { 
  if ( isset( $query->query_vars['Q1'] ) ){    
    echo ('hello');    
  }
}
share|improve this answer
    
AH HA Thanks , I knew i missed some thing !! –  user34416 Jun 22 '13 at 16:50
add comment

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.