wordpress get_categories function not working

I am trying to retrieve categories from the category taxonomy of the custom post type
By the below codes :

$categories = get_categories(array(
        'type'        => 'ad_listing',
        'child_of'    => 0,
        'parent'      => '',
        'orderby'     => 'name',
        'order'       => 'ASC',
        'hide_empty'  => 1,
        'hierarchical'=> 1,
        'exclude'     => '',
        'include'     => '',
        'number'      => '',
        'taxonomy'    => 'ad_cat',
        'pad_counts'  => false 
    ));
    echo"<pre>";
    print_r($categories);
    echo"</pre>";

But it is not showing nothing in category though there are 3 categories.
i think i am doing something wrong 🙁

Related posts

4 comments

  1. It will work,

      $cat_args = array(
                          'parent'  => 0,
                          'hide_empty' => 0,
                          'order'    => 'ASC',
                       );
        $categories = get_categories($cat_args);
        foreach($categories as $category){
           echo get_cat_name($category->term_id); 
        }
    
  2. Can you check any post assing in any category or not.If any post not assing any category then no category show.

    If you want to show all category without post assign to category. Change hide_empty=>false See below code

    <?php
    $categories = get_categories( array(
                'type'        => 'post',
                'child_of'    => 0,
                'parent'      => '',
                'orderby'     => 'name',
                'order'       => 'ASC',
                'hide_empty'  => false,
                'hierarchical'=> 1,
                'exclude'     => '',
                'include'     => '',
                'number'      => '',
                'taxonomy'    => 'category',
                'pad_counts'  => false 
            ) );
        echo"<pre>";
        print_r($categories);
        echo"</pre>";
    ?>
    
  3. Here is the code work for me.

    global $wpdb;
    $all_cats = array();
    $args = array(
        'taxonomy' => 'product_cat',
        'orderby' => 'name',
        'field' => 'name',
        'order' => 'ASC',
        'hide_empty' => false
    );
    $all_cats = get_categories($args);
    echo '<pre>';
    print_r($all_cats);
    

Comments are closed.