Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.

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

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

    Gdn::sql()->orderBy('score', 'desc'); //make the desc a variable if you want
    
    rloyola
  • 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

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    @donshakespeare good to have you here 😀


    @rloyola sort by Score, desc is a a built in sort. You can find this in the DiscussionModel:

       protected static $allowedSorts = [
           'hot' => ['key' => 'hot', 'name' => 'Hot', 'orderBy' => ['DateLastComment' => 'desc']],
           'top' => ['key' => 'top', 'name' => 'Top', 'orderBy' => ['Score' => 'desc', 'DateInserted' => 'desc']],
           'new' => ['key' => 'new', 'name' => 'New', 'orderBy' => ['DateInserted' => 'desc']]
       ];
    

    Try /discussions?sort=top


    rloyola
Sign In or Register to comment.