Expand the search results sorting filter

The search results sorting filter contains four sorting criteria options: title (post_title), price (price_from), rating (rating), available date (av_date_from).
However, the plugin also supports sorting by several more parameters: post publication date (post_date), post modified date (post_modified), menu order (menu_order).
Below is an example of adding a new sorting parameter to the filter (requires BA Book Everything 1.8.1 or higher).

add_filter('babe_search_filter_sort_by_args', 'child_theme_babe_search_filter_sort_by_args', 10, 1);
function child_theme_babe_search_filter_sort_by_args( array $filters ): array
{
    return array_merge( [
        'post_date_desc' => __( 'Sort by...', 'your-child-theme-textdomain' ),
        'post_date_asc' => __( 'Sort by...', 'your-child-theme-textdomain' ),
    ], $filters );
}

add_filter('babe_search_filter_to_get_posts_args', 'child_theme_babe_search_filter_to_get_posts_args', 10, 1);
function child_theme_babe_search_filter_to_get_posts_args( array $args ): array
{
    if( $args['search_results_sort_by'] === 'post_date_desc' ){
        $args['sort'] = 'post_date';
        $args['sort_by'] = 'DESC';
    }
    if( $args['search_results_sort_by'] === 'post_date_asc' ){
        $args['sort'] = 'post_date';
        $args['sort_by'] = 'ASC';
    }
    return $args;
}

add_filter('babe_search_result_args', 'child_theme_babe_search_result_args', 10, 1);
function child_theme_babe_search_result_args( array $args ): array
{
    if( empty($_GET['search_results_sort_by']) ){
        $args['search_results_sort_by'] = 'post_date_desc';
    }
    return $args;
}

Cookies

This site uses cookies to enhance your user experience and analyse traffic to improve this website. By clicking any link on this page you are giving your consent for us to set cookies. Please, see our Privacy Policy for additional information.
Scroll to top