The nav_menu_items_{$post_type_name}_recent WordPress PHP filter allows you to modify the posts displayed in the ‘Most Recent’ tab of the current post type’s menu items meta box.
Usage
add_filter( 'nav_menu_items_post_recent', 'your_custom_function', 10, 4 ); function your_custom_function( $most_recent, $args, $box, $recent_args ) { // Your custom code here return $most_recent; }
Parameters
- $most_recent (WP_Post[]): An array of post objects being listed.
- $args (array): An array of WP_Query arguments for the meta box.
- $box (array): Arguments passed to wp_nav_menu_item_post_type_meta_box().
- $recent_args (array): An array of WP_Query arguments for the ‘Most Recent’ tab.
More information
See WordPress Developer Resources: nav_menu_items_post_recent
Examples
Exclude specific categories from recent posts
add_filter( 'nav_menu_items_post_recent', 'exclude_categories_from_recent_posts', 10, 4 ); function exclude_categories_from_recent_posts( $most_recent, $args, $box, $recent_args ) { $exclude_categories = array( 3, 5, 7 ); // Category IDs to exclude $filtered_posts = array(); foreach ( $most_recent as $post ) { if ( !in_category( $exclude_categories, $post ) ) { $filtered_posts[] = $post; } } return $filtered_posts; }
Change the number of recent posts displayed
add_filter( 'nav_menu_items_post_recent_args', 'change_recent_posts_number', 10, 3 ); function change_recent_posts_number( $args, $box, $recent_args ) { $args['posts_per_page'] = 15; // Change the number of posts displayed return $args; }
Display only posts with featured images
add_filter( 'nav_menu_items_post_recent', 'only_featured_image_posts', 10, 4 ); function only_featured_image_posts( $most_recent, $args, $box, $recent_args ) { $featured_posts = array(); foreach ( $most_recent as $post ) { if ( has_post_thumbnail( $post->ID ) ) { $featured_posts[] = $post; } } return $featured_posts; }
Order recent posts by comment count
add_filter( 'nav_menu_items_post_recent_args', 'order_recent_posts_by_comment_count', 10, 3 ); function order_recent_posts_by_comment_count( $args, $box, $recent_args ) { $args['orderby'] = 'comment_count'; $args['order'] = 'DESC'; return $args; }
Show only posts from a specific author
add_filter( 'nav_menu_items_post_recent_args', 'show_specific_author_posts', 10, 3 ); function show_specific_author_posts( $args, $box, $recent_args ) { $args['author'] = 2; // Change the author ID return $args; }