Only show categories that have posts within custom post type

Try Below code:

    $args = array(
                    'post_type'      => 'work',
                    'posts_per_page' => -1,
                    'post_status'    => 'publish'
                );

    $the_query = new WP_Query( $args );
    $my_categories = array();
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $terms = get_the_terms( get_the_ID(), 'category' );
            if ( $terms && ! is_wp_error( $terms ) ) : 
                foreach ( $terms as $term ) {
                if(!in_array($term->term_id, $my_categories))
                    $my_categories[] = $term->term_id;
                }   
            endif;  
        }
        wp_reset_postdata();
    }

    if(sizeof($my_categories)) {
        foreach ($my_categories as $term_id) {
            $category = get_term_by('id', $term_id, 'category');
            if($category->slug!="all-articles") {
            ?>
                <button class="filter--item" data-filter=".<?php echo $category->slug; ?>"><?php echo $category->name; ?> <span class="checkbox"><i class="i-check"></i></span></button>
            <?php
            }
        }
    }

Hope this will help you!

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)