Add query string params/arguments to any WordPress URL
I am creating a custom page template that lists items from a "genre" taxonomy as a kind of sub menu. When you click on a genre, I want the person to stay on the custom page, but pass the chosen genre as a parameter.
Great for making custom listings of events, photo albums, or other custom types of posts.
Turns out there is cool method for this. Especially if you use it in combination with the standard get_permalink() function:
<?php
$genre_url = add_query_arg('genre', $term->slug, get_permalink());
# Outputs for example: http://my_wordpress.com/photos?genre=pop
?>
This way you don't have to parse get_permalink(), see if there are already query parameters in there. You just pass in the key-value pairs of what you would like to add and you pass in the original URL as the third argument.
See http://codex.wordpress.org/Function_Reference/add_query_arg for more info.
Written by Michiel Sikkes
Related protips
5 Responses

There are all sorts of weird and wonderful functions like this in the WordPress Codex, thanks for pointing this one out :)