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.

4
  • 3
    It’s not possible with the query loop block. You would need to manually add a query loop block for each category. Commented Feb 28, 2024 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. Commented Feb 28, 2024 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. Commented Feb 28, 2024 at 22:54
  • It doesn't seem like such a complex thing, on a code level it is doable with just a few lines, it seems more like an oversight or a lack of wanting to add native functionality. Anyway, I think you could extend the native ‘core/query’ block but I don't know exactly how to extend it and change the query. Commented Jan 15 at 16:27

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();
}
1
  • is not a good way to make a query, in case there is a lot of content it becomes slow, too many queries are executed Commented Jan 15 at 16:25

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.