Is there a way to add the list of recent posts into the admin sub menu on hover?

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:

Disclaimer: I am going to answer my own question.

Topic sub-menu admin posts menus Wordpress

Category Web


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
                );
            }
        }
    }
);

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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.