Preserve custom URL parameter on more pages

I created a custom URL parameter for sorting posts by their vote scores. I have a “most voted” link that sends a ?sort=most_voted URL paramater and using a query posts filter I display posts with most votes.

If for instance I want to display most voted posts in category 5, I’ll need a URL like this ?cat=5&sort=most_votes

How do I preserve/attach the sort parameter in the URL when browsing categories (or even by tag name, search, etc)?

Solutions Collecting From Web of "Preserve custom URL parameter on more pages"

You will need to intercept the links generated by WordPress and append the query var onto the relevant URLs.

You can do this quite easily with a filter on category URLs with something like…

function add_my_query_var( $link ) {
    $link = add_query_arg( 'sort', 'most_voted', $link );
    return $link;
}
add_filter('category_link','add_my_query_var');

I also spotted this handy list of filters on Mike’s post on SO, it should cover all the possible URLs you’d want to tweak the query vars for…

add_filter('page_link','add_my_query_var');
add_filter('post_link','add_my_query_var');
add_filter('term_link','add_my_query_var');
add_filter('tag_link','add_my_query_var');
add_filter('category_link','add_my_query_var');
add_filter('post_type_link','add_my_query_var');
add_filter('attachment_link','add_my_query_var');
add_filter('year_link','add_my_query_var');
add_filter('month_link','add_my_query_var');
add_filter('day_link','add_my_query_var');
add_filter('search_link','add_my_query_var');

add_filter('feed_link','add_my_query_var');
add_filter('post_comments_feed_link','add_my_query_var');
add_filter('author_feed_link','add_my_query_var');
add_filter('category_feed_link','add_my_query_var');
add_filter('taxonomy_feed_link','add_my_query_var');
add_filter('search_feed_link','add_my_query_var');

add_filter('get_edit_tag_link','add_my_query_var');
add_filter('get_edit_post_link','add_my_query_var');
add_filter('get_delete_post_link','add_my_query_var');
add_filter('get_edit_comment_link','add_my_query_var');
add_filter('get_edit_bookmark_link','add_my_query_var');

add_filter('index_rel_link','add_my_query_var');
add_filter('parent_post_rel_link','add_my_query_var');
add_filter('previous_post_rel_link','add_my_query_var');
add_filter('next_post_rel_link','add_my_query_var');
add_filter('start_post_rel_link','add_my_query_var');
add_filter('end_post_rel_link','add_my_query_var');

add_filter('previous_post_link','add_my_query_var');
add_filter('next_post_link','add_my_query_var');

add_filter('get_pagenum_link','add_my_query_var');
add_filter('get_comments_pagenum_link','add_my_query_var');
add_filter('shortcut_link','add_my_query_var');
add_filter('get_shortlink','add_my_query_var');

add_filter('home_url','add_my_query_var');
add_filter('site_url','add_my_query_var');
add_filter('admin_url','add_my_query_var');
add_filter('includes_url','add_my_query_var');
add_filter('content_url','add_my_query_var');
add_filter('plugins_url','add_my_query_var');

add_filter('network_site_url','add_my_query_var');
add_filter('network_home_url','add_my_query_var');
add_filter('network_admin_url','add_my_query_var');

Hope that helps..

You can make use of the add_query_arg() (WordPress Function) to add an additional parameter to the query (or also called query-info) part of an URL.

$baseUrl = '...'; // the url you want to add your parameter to
$sort = '...'; // the sort parameter you have
$sortUrl = add_query_arg('sort', $sort, $baseUrl);