How to group multiple wp_errors together?

You can retrieve all of the message of the same code with the get_error_messages() method (hacked from some code in the Codex):

function doer_of_stuff() {
   $err = new WP_Error( 'broke', "I've fallen and can't get up 1");
   $err->add('broke', "I've fallen and can't get up 2");
   $err->add('borken', "not this one");
   $err->add('borken', "not this one");
   $err->add('broke', "I've fallen and can't get up 3");
   return $err;
}

$return = doer_of_stuff();
if( is_wp_error( $return ) ) {
  //   var_dump($return);
  var_dump($return->get_error_messages('broke'));
}

I think that is what you want to do.

To get other messages just repeat the code: $return->get_error_messages('broke').

get_error_messages() with no argument will dump all of the messages:

if( is_wp_error( $return ) ) {
  //   var_dump($return);
  var_dump($return->get_error_codes());
  var_dump($return->get_error_messages());
}

Or just use the errors class variable itself:

$return = doer_of_stuff();
if( is_wp_error( $return ) ) {
  var_dump($return->errors);
}

The data is already “grouped”

tech