1

I am using Query Loop Block to list all posts under a custom post type. But I want to display all posts under each individual category, is that possible using query block?

Something like this:

Category 1: 
- Post 1
- Post 2
- Post 3

Category 2: 
- Post 4
- Post 5
- Post 6

Category 3: 
- Post 7
- Post 8
- Post 9

where categories are custom taxonomies for the post type, I dont want to manually add the category or create 3 custom query loop and filter for each taxonomy. I want to just use 1 block and list all posts under each category.

3
  • 3
    It’s not possible with the query loop block. You would need to manually add a query loop block for each category. Feb 28 at 5:00
  • 1
    @JacobPeattie yeah, I was wondering if there some sort of hook to modify the output but i think better way is just to create my own block.
    – Rain Man
    Feb 28 at 13:01
  • Not really. What you're trying to do is sufficiently different from the way the block normally works that it's probably not feasible. A custom block would be the way to go. Feb 28 at 22:54

1 Answer 1

-2

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.