Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  wpStudent Dec 1 '10 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). –  t31os Dec 1 '10 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 :) –  wpStudent Dec 1 '10 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). –  t31os Dec 1 '10 at 17:04
    
That's a very valuable suggestion. I'll definitely enable WP_DEBUG now that you mentioned it. –  wpStudent Dec 2 '10 at 0:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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