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.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYes 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.
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
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:
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
);
}
}
}
);