I added a filter to append a parameter onto the URL when navigating in categories. This is used to sort posts by their votes when browsing a category only if a sort
parameter is set.
For instance when you click view all posts with most votes, posts with high votes are displayed. From there you can view most voted posts by category by appending a sort=most_voted
or sort=doleast_voted
to the URL with cat=?
.
add_filter( 'category_link','append_parameter', 10, 2 );
function append_parameter( $link, $query ) {
$my_parameter = $query->query_vars['sort']; //get sort value
if ( isset($my_parameter) ) { //if browsing posts by votes
$link = add_query_arg( 'sort', $my_parameter, $link );
}
return $link;
}
I can’t figure out why the sort parameter isn’t appended to the URL. This works however without the if statements and a value instead of the $my_parameter
in the add_query_arg.
EDIT: New working code
add_filter( 'category_link','append_parameter', 10, 2 );
function append_parameter( $link, $my_parameter ) {
$my_parameter = $_GET['sort']; //get sort value
if ( isset($my_parameter) ) {
$link = add_query_arg( 'sort', $my_parameter, $link );
}
return $link;
}
If you take a look where the
category_link
hook is defined in category-template.php you’ll see this particular hook passes on two variables. The second variable is the category ID, but your callback function treats that second incoming variable as a query object.Simply put, you’re looking for a
query_vars
property/key that does not and cannot exist, because the incoming variable is not a query object.