get_query_var not works

Your filter:

function add_query_vars_filter( $vars ){
  $vars[] = "getvar";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );

Is correct. When WordPress starts assembling the list of query variables, this function will add ‘getvar’ to the list.

But then you immediately check if the variable is set before it reaches that point. The query_vars filter hasn’t happened yet. This suggests your understanding of events/filters is incomplete. When you add the filter, it doesn’t execute what’s inside, in the same way that if I tell you to eat food once you’ve paid, you wait until you’ve paid to eat it, you don’t immediately eat it.

So:

  1. You tell WordPress to execute a function when ‘query_vars’ filter happens
  2. You check if the illegal query var ‘getvar’ is present
  3. Later, the ‘query_vars’ filter happens and that function’getvar’ is made a legal safe query variable
  4. Nothing is done with this query variable

So you need to check for the query var after the function is executed, just being further down the file doesn’t do that. you should hook into an action such as init and perform your check there.