admin管理员组

文章数量:1125961

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.

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.

Share Improve this question asked Feb 28, 2024 at 3:30 Rain ManRain Man 1642 silver badges15 bronze badges 3
  • 3 It’s not possible with the query loop block. You would need to manually add a query loop block for each category. – Jacob Peattie 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. – Rain Man 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. – Jacob Peattie Commented Feb 28, 2024 at 22:54
Add a comment  | 

1 Answer 1

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

本文标签: Using Query Loop Block to list all posts under each category