With add_query_arg() it is nice to add additional parameters to an URL and it is also easy to create a rewrite rule. So far so good. But the problem is the URL replacement done by wordpress.

When I have an URL, like:


and I type this URL into the address line of the browser with activated permalinks. This link will become to:


BUT: what I would like to get is:


Finally, I would like to use code like:

<a href="<?php add_query_arg( 'myvar', 'test', get_permalink() ) ?>"> .... </a>

which would result in:


How can this be achieved? I have spent hours in reading documents and I couldn't found a solution.

  • 1
    I'd like to know how this is done. +1 to the question. Dec 27, 2013 at 21:04

3 Answers 3


Just faced the same situation and stumbled upon this question while googling.

It seems like this isn't possible. Core itself just appends strings to the URL if pretty permalinks are enabled, see https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/link-template.php#L571

For anyone interested in this: You can do something like this in your code:

if ( '' != get_option('permalink_structure') ) {
    // using pretty permalinks, append to url
    $read = user_trailingslashit( get_permalink() . 'test' ); // www.example.com/pagename/test
  } else {
    $read = add_query_arg( 'test', '', get_permalink() ); // www.example.com/pagename/?test

This solution is also was also recommended by Jon Cave in a comment on the official make blog: https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/#comment-686


The Wordpress solution would be to add rewrite rules to make something like this:

    add_action( 'init', 'my_rewrite_add_rewrites' );
    function my_rewrite_add_rewrites()
        add_rewrite_rule( 'calendar/([0-9]{4})/?$', 'index.php?post_type=events&year=$matches[1]', 'top' );
        add_rewrite_rule( 'calendar/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?post_type=events&year=$matches[1]&paged=$matches[2]', 'top' );

read more: https://codex.wordpress.org/Rewrite_API/add_rewrite_rule

  • 1
    You should have made it more complicated to downvote it... Mar 25, 2017 at 5:07

WordPress will not let you do this easily. In fact, rewrite rules are your only option to achieve this type of thing in WordPress and WordPress rewrite rules can get really messy when your site grows because plugins and themes (and you too) may add custom rewrite rules above existing rules in your (multi)site installation.

@swissspidy solution will work if you already have a rewrite rule for your key (myvar) and are using permalinks in the first condition and then fallback to traditional query strings in the absence of those conditions. @Johnathan Joosten solution shows how rewrite rules can become very very complex in WordPress (imagine doing that for all your query string keys and values...)

I met this issue a few times before while working on some mid-size projects for my clients and I was able to achieve something similar using a plugin called Obfusquer. According to the website, it masks all WordPress query strings with a user defined secret keys and also provides hooks for advanced custom requirements.

Funny, it is not listed on the WordPress plugin repository, and I think you will need a subscription (which I think is really affordable since 1 single license will work even in multisite installation) but here is a link to it if you needs to mask/hide query strings in WordPress installations. They also have documentation here.

I now use the plugin in almost all my wordpress projects, hope it helps your needs. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.