My question here is, which of the arguments are responsible for the
correct ordering of the children under the parent taxonomy category?
None — the orderby
arg only sorts the categories and not grouping them (placing them under their own parent), so you would need to manually do the grouping.
And setting orderby
to parent
will only put all parent categories at the top, then followed by child categories, just like your example in the question..
But instead of manually grouping the categories..
You could actually simply use wp_dropdown_categories()
to get the hierarchical structure/display:
$args = array(
'taxonomy' => 'my_custom_taxonomy',
'hide_empty' => false,
'echo' => false,
'hierarchical' => true,
'show_count' => true,
'orderby' => 'parent',
'id' => 'categories_dropdown',
'value_field' => 'slug',
);
$select = wp_dropdown_categories( $args );
And that would give you a select
menu with the markup/HTML similar to the one in the question.
However, if you need to add custom HTML like data-xxx
attribute to the <option>
tag, then you can use your own custom walker class (see Walker_CategoryDropdown
) or try the recursive function in my answer on Stack Overflow.