0

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');    
  }
}
0

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');    
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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