Group and list posts by custom taxonomy

I re-factored your code, there were quite some parts which seemed overly complex or used deprecated methods.

In the code below, I’m looping through your terms, and within that loop, I’m finding the posts relating to that term. Probably there’s a more efficient way, since this is using several queries, but it should achieve the desired result.

<?php
// get the terms, ordered by name
// https://developer.wordpress.org/reference/functions/get_terms/
// https://developer.wordpress.org/reference/classes/wp_term_query/__construct/
$taxonomy = 'date-exposition';
$tax_terms = get_terms( 
  array(
    'taxonomy' => $taxonomy,
    'hide_empty' => false, // change to true if you don't want empty terms
    'orderby' => 'name',
    'order' => 'DESC',
    'fields' => 'names', // return the term names only
  )
);

foreach($tax_terms as $tax_term) { // loop through the terms
  echo '<h2>' . $tax_term . '</h2>'; // echo the term name as a h2
  $term_posts = get_posts( // find posts with the correct term
    array(
      'no_found_rows' => true, // for performance
      'ignore_sticky_posts' => true, // for performance
      'post_type' => 'exposition',
      'posts_per_page' => -1, // return all results
      'tax_query' => array( // https://developer.wordpress.org/reference/classes/wp_tax_query/
        array(
              'taxonomy' => $taxonomy,
              'field'    => 'name',
              'terms'    => array( $tax_term )
          )
      ),
      'fields' = 'ids', // return the post IDs only
    )
  );
  
  echo '<ul>'; // open bullet list
  foreach($term_posts as $term_post_id) { // loop through posts
    $post_title = get_the_title($term_post_id); // get post title
    $post_permalink = get_the_permalink($term_post_id); // get post link
    echo '<li>'; // open list item
    echo '<a href="' . $post_permalink . '">' . $post_title . '</a>'; // add link to post with post title as link text
    echo '</li>'; // close list item
  }
  echo '</ul>'; // close bullet list
}
?>

https://codeshare.io/Jbyqvd

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