There are a million posts on the topic in this stack exchange, and yet for some reason I cannot get this to work for me. I wonder what I'm doing wrong?
In my functions.php I define this:
add_filter('query_vars', 'add_quote_query_vars');
function add_quote_query_vars($qv){
$qv[] = 'xx-quote';
return $qv;
}
Then later, on front_page.php I provide a link that embeds this into the URL:
echo '<a href="'.add_query_arg('xx-quote', $quote->ID).'">';
Two things go wrong.
1: if, on my template page I print_r($wp_query->query_vars);
, my 'xx-quote' doesn't appear in the list
2: as soon as I actually try to include that in the URL (by clicking the link, the URL is http://mysite.com/blog/?xx-quote=555
) I'm taken to an archive page, rather than the expected front page.
What's interesting about #2 is that as soon as I remove the query_vars
filter and click the link again, I'm taken correctly to the front page as expected.
Am I just not understanding the role of the query_vars? Is this expected behaviour? Or am I calling the filter at the wrong time (I've tried wrapping it in an init action, or even bare inside functions.php)?
I know I can just ignore all this, since I'm dealing with the front_page.php anyway, and just use $_REQUEST['xx-quote'] but that doesn't seem like a best practice at all, and I'm trying to do it "the WordPress way"...
Needless to say, the following code produces no results:
global $wp_query;
echo $wp_query->query_vars['xx-quote'];
And that is the basis of my confusion. I've read all the related posts on this SE and I seem to be doing everything right. Clearly I'm not - what's gone wrong?