Custom sub taxonomy order by

It’s a little bit hard to say what exactly was the point of your code…

First problem that I can see is that you’re misusing get_term_by function.

This function takes 5 params:

get_term_by( $field, $value, $taxonomy, $output, $filter )
  • $field (string) (required) Either ‘id’, ‘slug’, ‘name’, or ‘term_taxonomy_id’

  • $value (string|integer) (required) Search for this term value Default: None

  • $taxonomy (string) (required) Taxonomy Name category, post_tag, link_category, nav_menu or something custom Default: ” (empty string)

  • $output (string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N Default: OBJECT

  • $filter (string) (optional) default is raw or no WordPress defined filter will applied. Default: ‘raw’

And in your code it’s used like so:

$term = get_term_by( 
    'id', 
    $child, 
    'event-categories', 
    array( 
        'orderby' => 'slug',
        'hide_empty' => true
    ) 
);

So first three params are correct, but the last one should be $output and you pass something else in there… And even if that array was correct param, it makes no sense to sort in that place…

So how to get children of a term sorted in alphabetical order?

You can do this easily using get_terms function:

$term_children = get_terms( array(
    'taxonomy' => 'event-categories',
    'child_of' => $EM_Category->id,
    'orderby' => 'name'
) );