List non-empty categories from a custom post type

This will end up being a little convoluted since the taxonomy just describes a relationship that may belong to any of the post types, so there’s not a direct data link for what you’re trying to fetch.

But you could do something like this (untested), which could be a fairly heavy query if you’re doing on every page load or something, so maybe consider running it on a cron job and storing it somewhere or something.

<?php

    $args = array(
        'post_type' => 'my-post-type',
        'posts_per_page' => -1,
    );

    $fetch = get_posts( $args );
    $cat_r = array();

    // Iterate the posts and get the categories
    foreach( $fetch as $item ) {
        $cats = get_the_terms( $item->ID, 'category' );
        $cat_r[] = $cats;
    }

    // Outputs an array of term objects
    echo '<pre>';
    print_r( $cat_r );
    echo '</pre>';