0

If I need to edit a specific post then I need to click the "All Posts" link and then find it. It would be nice to click directly on a link from within the popup hover sub menu.

Something like this:

An admin sub menu with list of recent posts entries

Disclaimer: I am going to answer my own question.

2 Answers 2

1

Yes you can. By hooking into the admin_menu hook you can add sub menu pages, and they will appear on hover by default. All you have to do is iterate the recent posts and add them one by one.

add_action('admin_menu', function () {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'orderby' => 'post_date',
        'sort_column' => 'post_date',
        'sort_order' => 'asc',
        'posts_per_page' => 10,
    );
    $arr = get_posts($args);
    foreach ($arr as $item) {
        add_submenu_page(
            'edit.php',
            $item->post_name,
            '• ' . $item->post_title,
            'read',
            'post.php?post=' . $item->ID . '&action=edit',
            ''
        );
    }
});

Update: I wrote a plugin for this. It also shows pages and custom post types. Hope that helps.

2
  • 1
    the code here has a performance hit because it uses get_posts and doesn't set the suppress filters option to false, it's all uncached, and can't be filtered or adjusted by plugins. It also looks like you're unaware of the post edit link functions
    – Tom J Nowell
    May 26, 2022 at 11:43
  • Thank you. I tried to keep it simple indeed.
    – IT goldman
    May 26, 2022 at 15:12
0

Yes!

On the admin menu hook, grab all the post types using get_post_types, then for each type, do a query for posts of that type, and call add_submenu_page with their edit URLs to put them in the menu.

Here's a more comprehensive alternative to IT Goldmans answer with the following additions:

  • fixes a performance and caching issue that stopped it working with caching plugins and plugin filters
  • works for all post types
  • adds a separator and puts them at the end
  • edit icons!
  • handles posts with no title
  • respects the posts_per_page setting and default ordering
  • doesn't break custom edit links

enter image description here

To use, copy paste to a new PHP file in the plugins folder:

<?php
/**
 * Plugin Name: Basic admin post submenus
 * Plugin Author: Tom J Nowell
 */

// https://wordpress.stackexchange.com/questions/406113/is-there-a-way-to-add-the-list-of-recent-posts-into-the-admin-sub-menu-on-hover


add_action(
    'admin_menu',
    function () : void {
        $types = get_post_types( [
            'public' => true,
        ] );

        foreach( $types as $type ) {
            $parent_url = add_query_arg(
                [
                    'post_type' => $type === 'post' ? null : $type
                ],
                'edit.php'
            );

            $args = [
                'post_type'        => $type,
                'post_status'      => 'publish',
                'suppress_filters' => false,
            ];
            $posts = get_posts( $args );
            if ( empty( $posts ) ) {
                continue;
            }

            add_submenu_page(
                $parent_url,
                'wp-menu-separator',
                '',
                'read',
                '',
                '',
                11
            );

            foreach ( $posts as $post ) {
                $title = ! empty( $post->post_title ) ? $post->post_title : '<em>' . __( 'Untitled' ) . '</em>'
                add_submenu_page(
                    $parent_url,
                    $post->post_name,
                    $title . '<span class="dashicons dashicons-edit"></span> ',
                    'read',
                    get_edit_post_link( $post->ID ),
                    '',
                    11
                );
            }
        }
    }
);

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.