Post sort override for ACF Relationship posts

I followed the guide here Create a filter to customize the display of post data | Beaver Builder Knowledge Base
for adjusting the wp_query and added a ID to the post module as well.
But it doesn’t have an effect on the sorting ‘orberby’. At the moment it is still using the sort set in the post module.
Is there something else I need to do to override sort ACF relationship post?
The end goals is to actually pass an array to sort by player_number and then by title. but I can’t get this to work by itself…

function player_fl_builder_loop_query_args_filter( $query_args ) {
    // target this post module ID
if ( 'player_list' == $query_args['settings']->settings->id ) {
    $query_args['meta_key'] = 'player_personal_player_number';
    $query_args['orderby'] = 'meta_value_num';   // sort by this
    
  }

  return $query_args;
}

I found the error in the if

The correct code is this , which now has the working orderby array as well.

add_filter('fl_builder_loop_query_args', 'player_fl_builder_loop_query_args_filter');

function player_fl_builder_loop_query_args_filter($query_args) {
    // Target this post module ID
    if ('player_list' === $query_args['settings']->id) {      // <-- this bit
        $query_args['meta_key'] = 'player_personal_player_number';
        $query_args['orderby'] = array(
            'meta_value_num' => 'ASC', // Order by 'player_personal_player_number' numerically
            'title' => 'ASC', // Then order by post title alphabetically
        );
    }

    return $query_args;
}

The 2nd example on the docs page is for the search module, it has the settings in a slightly different format, the other example is the one you needed :wink: Anyway you figured it out.

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.