1
votes

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 :(

4
Do you have any posts in those categories? hide_empty isn't showing categories without posts attached to them. - Ohgodwhy

4 Answers

3
votes

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); 
    }
1
votes

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>";
?>
0
votes

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);
0
votes

Use <?php get_taxonomies( $args, $output, $operator ) ?> get_categories might be for post_type='post'

https://codex.wordpress.org/Function_Reference/get_taxonomies