How to get pretty URLs with add_query_arg in permalinks

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

Leave a Comment