4

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)?

  • Not sure what you mean by "preserve" the sort parameter. Do you mean "How do I parse a URL such that it generates a list of posts on a category page using the desired sort order?" – MikeSchinkel Nov 29 '10 at 6:31
  • Yes, I want to keep sort in the URL when navigating away, in a category. – wpStudent Nov 29 '10 at 14:10
  • Why don't you past the code your using on your template and functions file so we can review the whole thing. – NetConstructor.com Nov 29 '10 at 16:53
3

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..

  • Brilliant answer! Thanks this should put me on the right path :) – wpStudent Nov 29 '10 at 19:36
  • Btw, is it wise to create the functions before defining the filters? I have mine vice-versa – wpStudent Nov 29 '10 at 19:41
  • 2
    @wpStudent: It does not matter. The function needs to exist when the hook is executed, not when you assign it. I also do hook-then-function, it makes it easier to follow if you have long functions. – Jan Fabry Nov 29 '10 at 22:23
  • +1 for what Jan said... :) I tend to place the action prior to the function(FWIW), but when giving an example i don't really fuss about smaller details... ;) – t31os Nov 30 '10 at 10:11
  • And if you put the names of the all the hooks into an array you can iterate over them with foreach. If the array is then returned by a function, this is even mor easy for future use. – hakre Nov 30 '10 at 11:20
2

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);
  • Thanks for sharing!! I wasn't aware of that WP function. – wpStudent Nov 29 '10 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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