Get WordPress Category ID from a Post ID

I am using a custom post type and custom categories. I need to fetch the category ID of a custom post type in a page. I tried using various methods but I am unable to get the category ID.

<?php 
    $args = array(
                'posts_per_page'=> -1,
                 'post_type' => 'professional',
                 'orderby'=> 'id',
                 'order'=> 'asc',
                );
            query_posts($args);

    $my_posts = new WP_Query($args);
         while ($my_posts->have_posts()) : $my_posts->the_post(); // loop for posts
    function getCurrentCatID(){
    global $wp_query;
    global $post;
    $cats = get_queried_object()->term_id;
    $cat_ID = $cats[0]->cat_ID;
    return $cat_ID;
    } 
?>
<li>
    <div class="caption">
        <h3><a href="<?php  the_permalink(); ?>"><?php the_title();  ?></a></h3>
        <span class="industry"><?php getCurrentCatID($cat_ID); ?></span>
    </div>
</li>

I am not able to get the cat ID in custom post types.

Related posts

Leave a Reply

1 comment