I am trying to link out from the WP admin to view posts in a specific way. The url structure for this is example.com/post123/?my-preview=456

/post123/ is the regular permalink for a post. The ?my-preview=456 allows a section of that post to retrieve information based off of the my-preview value.

I understand that WP strips $_GET parameters from the url, so I tried using add_filter('query_vars','my_query_vars'); and

function my_query_vars($query_vars){
    $query_vars[] = 'my-preview';
    return $query_vars;

On the actual section where I need to get that value, I tried using:

global $wp_query;

Even with a url like the example above, the 'my-preview' pair is nowhere to be found in the $wp_query->query_vars array.

Am I not doing the correct steps to register an additional query_var for use later on? If I am, why does it not stick around?

EDIT: I have also tried: add_action('init', 'add_query_vars');

function add_query_vars() {
    global $wp;

and wp_die(var_dump(get_query_var('my-preview'))); gives string(0) ""

  • Thank you for your comment. 1. I am not seeking to extend pretty permalinks. I just want to use $_GET style parameters. 2. I have tried the methods listed in the accepted answer to no avail. I will update my question with that code. – Josh Levinson Jul 23 '13 at 20:18

The query vars filter is unnecessary for what you're doing here, as you're not using it in a query. Adding it to the URL as a GET parameter will not make it appear in the array of query vars, as there's no mechanism that converts a GET var to a query var, you've only ensured it won't be removed if it's added to the query.

As long as the GET var is unique and not reserved by WordPress, there's nothing that should prevent setting and then accessing a GET var via normal means: $_GET['my-preview'].

  • According to codex.wordpress.org/… , parse_request() strips the $_GET parameters from the URL. When I visit example.com/post123/?my-preview=123 (I doubt my-prevoew is reserved), wp strips the get params and I eventually land on example.com/post123/ – Josh Levinson Jul 24 '13 at 4:23
  • Also, you said there is no mechanism for converting get params into query bars, but that's exactly what step 4 or parse_request does according to that page. – Josh Levinson Jul 24 '13 at 4:28
  • If query_vars is not the solution, why does wp strip my get params, and what can I do about it? – Josh Levinson Jul 24 '13 at 4:30
  • parse_request strips get vars for the purpose of parsing the request, it doesn't redirect to that version of the URL. why it doesn't work for you I can't say, I do this all the time without issue- I've just tested on a clean install and twentytwelve theme, and it works as expected. – Milo Jul 24 '13 at 4:37
  • Hm...I'll have to try this out and get back on it. – Josh Levinson Jul 24 '13 at 14:37

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.