Listing all Wordpress Posts

I wanted a list of all wordpress posts in a single lists. I used the plugin “List category posts” by Fernando Briano which worked as described but I had to add each category. If new categories were added it involved updating the page.

List Posts by Category

I posed the problem to ChatGPT and through a number of iterations arrived at this plugin.

Listing of all Wordpress Posts Plugin Code
<?php
/*
Plugin Name: List Posts by Category
Description: A simple plugin to list posts grouped by category using a shortcode.
Version: 1.0
Author: AnythingAccess.com
Author URI: https://AnythingAccess.com
*/

function list_posts_by_category_with_summary() {
    $categories = get_categories(array(
        'hide_empty' => true, // Only retrieve categories with posts
    ));
    $output = '';

    // Generate the summary of categories with post counts and links
    $output .= '<h4>Category Summary</h4>';
    $output .= '<ul>';
    foreach ($categories as $category) {
        $output .= '<li><a href="#' . esc_attr($category->slug) . '"><strong>' . esc_html($category->name) . '</strong></a> (' . $category->count . ' posts)</li>';
    }
    $output .= '</ul>';

    // Generate the detailed list of posts by category
    foreach ($categories as $category) {
        $output .= '<h5 id="' . esc_attr($category->slug) . '">' . esc_html($category->name) . '</h5>';
        $posts = get_posts(array(
            'category' => $category->term_id,
            'numberposts' => -1, // Retrieve all posts in the category
        ));

        if ($posts) {
            $output .= '<ul>';
            foreach ($posts as $post) {
                $output .= '<li><a href="' . get_permalink($post->ID) . '">' . esc_html($post->post_title) . '</a></li>';
            }
            $output .= '</ul>';
        }
    }

    return $output;
}
add_shortcode('list_posts_by_category', 'list_posts_by_category_with_summary');


By ading the shotcode “list_posts_by_category” on any page it will list the posts summary and hyperlink to the details as shown below.

Category Summary

Access
Accounting
Agresso
ChatGPT
Design
ETL
Excel
Outlook
Ribbon
Sage
SAP
SQL
VBA
Wordpress
,

Leave a Reply

Your email address will not be published. Required fields are marked *