HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
How to sort discussions based on the discussion table column

Hi
I want to sort the discussions based on table column Score, my discussion table has Score entries
when I check this url somesite/discussions?sort=score nothing has changed. Any Ideas why this is not working?
In my plugin I have this code
public function discussionModel_beforeGet_handler($sender,$args){ $sender->addSort( 'score', 'Score', [ 'Score'=>'desc', ]); $sender->setSort('score'); }
0
Comments
Read this entire thread. Some very very very goods inside
https://open.vanillaforums.com/discussion/32730/any-example-of-using-the-new-sort-filter-module
If you get stuck, do a $_GET (the vanilla f. way), match the key "score" and run this code
Thanks for pointing me into the right direction. I also use the DiscussionsController_BeforeBuildPager_handler in my plugin to add the query string in the pager
@donshakespeare good to have you here 😀
@rloyola sort by Score, desc is a a built in sort. You can find this in the DiscussionModel:
Try /discussions?sort=top
@rloyola did you ever find a solution?
I'd like to use the native
addSort
since my suggested answer fails woefully when across pagination.Anybody have any thoughts on the correct way, the right function/event to fire this addSort thing?
Cheers!
Try that one:
@donshakespeare
I get the query string from the url and use it with Gdn::sql()->orderBy in discussionModel_beforeGet_handler and discussionModel_BeforeGetCount_handler
$sortValue = Gdn::request()->get('sort');
if($sortValue == 'some_sort_value'){
Gdn::sql()->orderBy('some_discussion_column', 'desc');
}
Then I update the pager query parameters using DiscussionsController_BeforeBuildPager_handler
$defaultParams = Gdn::request()->get();
$Page = GetValue(0, $args, 'p1');
$sender->setData('_PagerUrl', 'discussions/{Page}'.'?'.http_build_query($defaultParams));
Let's just say I was over thinking the issue. I dislike when that happens; it can take me up to 5hrs to realize the room is dark because of my expensive sunglasses.
For the record, @R_J that is exactly where to use
addSort
- works a charm!@rloyola thanks for knocking off my sunglasses and grounding me. I shall be using JS to fine-tune that part