How to List WordPress Posts Grouped by Tag or Category
If you want to display WordPress posts grouped by category or tag, here are the best ways:
1️⃣ Use a Plugin (Easy Method)
If you prefer a no-code solution, try Display Posts or WP Show Posts plugin.
🔹 Steps to Use Display Posts Plugin
- Install & Activate Display Posts plugin.
- Use this shortcode in a post/page:
[display-posts category="your-category" include_date="true"]
- Replace
"your-category"with the actual category slug.
2️⃣ Use Custom PHP Code (For Theme or Custom Template)
If you want to list posts grouped by category dynamically, add this PHP code to your theme template (e.g., page.php, sidebar.php, or a custom template):
🔹 Group Posts by Category
<?php
$categories = get_categories();
foreach ($categories as $category) {
echo '<h2>' . esc_html($category->name) . '</h2>';
$posts = get_posts(array(
'category' => $category->term_id,
'posts_per_page' => -1
));
foreach ($posts as $post) {
echo '<a href="' . get_permalink($post->ID) . '">' . esc_html($post->post_title) . '</a><br>';
}
}
?>
📌 This will list posts under each category with clickable links.
🔹 Group Posts by Tag
<?php
$tags = get_tags();
foreach ($tags as $tag) {
echo '<h2>' . esc_html($tag->name) . '</h2>';
$posts = get_posts(array(
'tag' => $tag->slug,
'posts_per_page' => -1
));
foreach ($posts as $post) {
echo '<a href="' . get_permalink($post->ID) . '">' . esc_html($post->post_title) . '</a><br>';
}
}
?>
📌 This will list posts grouped by tag.
3️⃣ Display Posts Grouped by Category in a Gutenberg Block
If you’re using Gutenberg Block Editor, you can use the Query Loop block:
- Go to Edit Page/Post → Click “+” to add a block.
- Search for Query Loop and insert it.
- Choose “Inherit query from template” or select a specific category/tag.
- Customize layout and styles.
🎯 Best Option for You?
- ✅ Use a plugin if you want a fast and easy way (e.g., Display Posts).
- ✅ Use PHP Code if you need full customization in your theme.
- ✅ Use Gutenberg Query Loop Block for a native WordPress approach.
Would you like help styling the output for a better look? 😊
Leave a Reply