1

In single categories page, particular parent categories sub-categories display by slug or order wise i will try but something is wrong in my code so please check my code and let me know how is it possible.

$term_children = get_term_children( $EM_Category->id, 'event-categories' );
if ( count($term_children) > 0 ) {
    foreach ( $term_children as $child ) {
        $term = get_term_by( 
            'id', 
            $child, 
            'event-categories', 
            array( 
                'orderby' => 'slug',
                'hide_empty' => true
            ) 
        );
    }
}
0

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'
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.