Unable to Retrieve Query Parameters Passed in URL

There’s been a misunderstanding of what query vars actually are and what they’re used for.

The correct way to read a URL parameter is the same way as every other PHP application:

$mc_id = $_GET['mc_cid'];

This is because query vars are not URL parameters! WordPress doesn’t have a special way to retrieve URL parameters, it works the same way as every other PHP application, $_GET and filter_input.

What If $_GET is blank?

If $_GET is blank then it’s not because you’re meant to use a special method to access the parameters, it’s probably because:

  • The URL with the parameters is redirecting and not preserving those parameters in the new URL
  • A plugin or theme is wiping $_GET, either intentionally or accidentally
  • The server is misconfigured at the Apache/Nginx level and not passing those values to PHP ( unlikely as you’d notice other issues and could easily test this by doing a search /?s=...

The precise reason isn’t something that can be figured out from just what’s in your question right now.

Then What Are Query Vars?

Query variables instead are a part of rewrite rules and WP_Query. A query var is a parameter that was passed to the main post query aka the WP_Query that the main loop runs off of, that powers the conditionals such as is_single/etc and determines the main template.

So when you do this the s is a query variable:

$q = new WP_Query( [ 's' => 'search terms' ] );

The confusion happens because in ugly permalinks the main post loop that determines the template and posts etc takes its arguments from the URL parameters. This is also why rewrite rules are regex’s that map pretty permalinks into ugly index.php?var=value type URLs.

Additionally, the method used to add a query var that you used is also incorrect. Instead you would use the query_vars filter to add it to the whitelist, you would never call functions on the $wp global!

So if you’re meant to use $_GET/filter_input why do these functions exist?

  1. Sometimes you need to grab a value from the main query, e.g. get_query_var( 'meta_query' )
  2. When using custom rewrite rules to create pretty URLs, sometimes it’s helpful to add a custom query variable, e.g. for virtual pages that don’t have an associated post or perform an action. Then you can look for these and use specific values to trigger code, but you would never see them in the URL itself

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino