Group custom posts by custom taxonomy names

You should write it as the following:

$term_ids = array_map( function( $t ) { return $t->term_id ; }, $terms );

Also in the code, you have the get_terms where you directly write the taxonomy name. This has been deprecated in favor of the following :

$terms = get_terms( array(
    'taxonomy' => 'event-categories',
    'hide_empty' => false,
) );

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument
in the $args array:

Second Question

About why you have an empty posts:

You are searching by default in the posts with post_type=post.

You should search for your custom post_type like this:

$posts = get_posts(
    array(
        'post_type' => 'custom_post_type',
        'nopaging'  => true,
        'tax_query' => array(
            array(
                'taxonomy' => 'event-categories',
                'terms'    => $term_ids ,
        ) ),
) );

I removed the 'field' => 'id'
Because by default it gets the term_id and the accepted values are :

  • ‘term_id’
  • ‘name’
  • ‘slug’
  • ‘term_taxonomy_id’