1

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;
    }

1 Answer 1

2

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.

5
  • thank you for the info! Not sure if I'm on the same page. Hopefully this is relevant to your answer.. I'm not getting any query object related errors. I use the same approach in other functions. The $query argument is for $_GET["sort"] which checks the value of my sort parameter that I added with a query_vars filter. If for instance $_GET["sort"] = "most_voted" I want to display all posts with high votes within any category. Commented Dec 1, 2010 at 2:03
  • 1
    My point being, the second variable in your function $query is not a query object.. it's a category ID passed along from the filter your function is hooked onto(the filter hook sets what is in this variable). Commented Dec 1, 2010 at 11:46
  • Ahh thanks for explaining!! I guess the second query is ideal for post filters.. I added a new working code in my answer. I think I'm on the right track now :) Commented Dec 1, 2010 at 15:36
  • 1
    If you're not going to use the value being passed into that second variable, just drop it from the filter.. NOTE: You should ideally check a var is set before storing it in a variable(i'd suggest writing your plugins with WP_DEBUG enabled). Commented Dec 1, 2010 at 17:04
  • That's a very valuable suggestion. I'll definitely enable WP_DEBUG now that you mentioned it. Commented Dec 2, 2010 at 0:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.