In bbPress this is to change the order of the replies under each topic and add a links to select the order.
<?php
function bbp_reverse_reply_order( $query = array() ) {
$query['order'] = 'DESC';
if( isset($_GET['order']) && in_array($_GET['order'],['desc','asc']) ){
$query['order'] = $_GET['order'];
}
return $query;
}
add_filter('bbp_has_replies_query','bbp_reverse_reply_order');
function bbp_add_order_topic_buttons(){
echo '<div style="text-align: right; margin: 10px;">Sort by: <a href="' . add_query_arg(['order' => 'desc'], $wp->request) . '">Recent first</a> | <a href="' . add_query_arg(['order' => 'asc'], $wp->request) .'">Older first</a></div>';
}
add_action('bbp_template_before_replies_loop','bbp_add_order_topic_buttons');