Loop through categores and posts of a custom post type in WordPress?

You can try this code logic and see if it works.

// Get all categories
$category_args = array(
    'orderby' => 'name',
    'order' => "ASC",
);
$categories = get_categories( $category_args ); // Get all Categories

// Loop through each category
foreach ($categories as $category) {
    
    // Display the category title
    echo '<h2>' . $category->name . '</h2>';
    
    // Fetch all posts that has that category
    $args = array(
        'post_type' => 'faq',
        'post_status' => 'publish',
        'posts_per_page' => -1, // Get all posts
        'category__in' => $category->term_id // Filter by category
    );

    $query = new WP_Query($args);

    // Check if there are posts for this category
    if ($query->have_posts()) {
        echo '<ul>';
        // Loop through the posts 
        while ($query->have_posts()) {
            $query->the_post();
            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo '<p>No posts found for this category.</p>';
    }

    // Reset post data
    wp_reset_postdata();
}

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