Skip to content

Filter: bricks/query_filters/sort_query_vars

Filters the query variables generated by an active “Sort” filter element. This allows you to implement custom sorting logic (e.g., sorting by a custom meta key or a calculated value).

  • $query_vars (array): The WP_Query arguments generated by the sort filter (e.g., ['orderby' => '...', 'order' => '...']).
  • $filter (array): The active filter data, including selected sort option.
  • $query_id (string): The ID of the query loop being sorted.
  • $filter_index (int): The index of this filter in the active filters list.
add_filter( 'bricks/query_filters/sort_query_vars', function( $query_vars, $filter, $query_id, $filter_index ) {
// Example: Custom sort by 'popularity'
if ( $filter['value'] === 'popularity' ) {
$query_vars['orderby'] = 'meta_value_num';
$query_vars['meta_key'] = 'post_views_count';
$query_vars['order'] = 'DESC';
}
return $query_vars;
}, 10, 4 );