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:
function add_query_vars_filter( $vars ){
$vars[] = "pid";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
<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>
<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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With