Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress get_query_var() not working

I have been trying to get add_query_arg() and get_query_var() to work in my custom Wordpress theme for hours with no success.

I have searched and tried various 'resolutions' to no avail, and cannot work out what I am missing, any help would be appreciated.

I am trying to create a custom edit post page and looking to pass a URL query var of ?pid= variable to pass in the post ID. I currently have:

functions.php

function add_query_vars_filter( $vars ){
    $vars[] = "pid";
    return $vars;
}

add_filter( 'query_vars', 'add_query_vars_filter' );

content-single.php

<a href="<?php echo add_query_arg( array('pid' => get_the_ID()), get_site_url() . '/new-post' ) ?>">
    <i class="fa fa-pencil" aria-hidden="true"></i> Edit Post
</a>

page-new-post.php

<div>
    <?php echo 'pid: ' . get_query_var('pid', 'Not Set'); ?>
</div>

The result is always Not Set. I have tried other variable names to ensure avoiding any Wordpress default vars, but none work.

Is there anything else I need to add to enable custom vars, or am I making a silly typo or anything?

like image 787
MarkJWiggins Avatar asked Apr 29 '26 19:04

MarkJWiggins


1 Answers

This post is a little old, but here is a solution.

So, I had the same problem and it was an error in my nginx configuration file. The solution is to pass the $query_string variable in my nginx like :

location / {
   try_files $uri $uri/ /index.php?$query_string;
}

If you have an Apache configuration, try searching for a similar solution.

like image 57
Brukols Avatar answered May 04 '26 11:05

Brukols