I’m trying to have custom variables in my URL for WordPress site. I have read up as much as I could find on the subject and so far have the following in my functions page:
function add_query_vars($aVars) {
$aVars[] = "mrdrct";
return $aVars;
}
// hook add_query_vars function into query_vars
add_filter('query_vars', 'add_query_vars');
And the following on my header page:
if(isset($wp_query->query_vars['mrdrct'])) {
$mVar = $wp_query->query_vars['mrdrct'];
echo "variable is $mVar <br />";
}
Just to test out if things are being passed correctly and they are. However, when I use a link with the url variable in it – say www.mydomain.com/?mrdrct=myVarable – I am not directed to my homepage of my WordPress site which is set to a static page with a template on it – I am instead directed to a page with my latest posts on it. I cannot figure out why this is happening – any ideas? Hopefully I’ve explained this well enough.
Thanks.
When WP sees a query string (
?
after the URL) it will attempt to display matching posts using it’s rewrite rules. If no posts match it will show a 404 error – I would guess you do not have a 404.php file, so WP is showing the default which is index.php (see the WordPress Template Hierarchy for more details on that).I’m not 100% sure what you want to achieve, but I’d suggest that you need to look at changing the query when
$wp_query->query_vars['mrdrct']
is set. See the WP Codex forquery_posts()
for a good place to start, if you are not already familier with it.