Use WP_Query
directly in your WP theme template files instead of a block editor.
// Define custom taxonomy (category) terms
$categories = get_terms(array(
'taxonomy' => 'your_custom_taxonomy', // Change 'your_custom_taxonomy' to the name of your taxonomy
'hide_empty' => false,
));
// Loop through each category
foreach ($categories as $category) {
echo '<h2>' . $category->name . '</h2>'; // Display category name
echo '<ul>';
// Query posts based on the current category
$category_query = new WP_Query(array(
'post_type' => 'your_custom_post_type', // Change 'your_custom_post_type' to the name of your custom post type
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy', // Change 'your_custom_taxonomy' to the name of your taxonomy
'field' => 'term_id',
'terms' => $category->term_id,
),
),
));
// Loop through posts in the current category
while ($category_query->have_posts()) {
$category_query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; // Display post title with permalink
}
echo '</ul>';
// Reset post data
wp_reset_postdata();
}