Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#41508 closed defect (bug) (invalid)

wp_get_nav_menu_items() does not account for posts_per_page — at Version 1

Reported by: compute's profile Compute Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8
Component: Menus Keywords:
Focuses: template Cc:

Description (last modified by welcher)

When passing $args['posts_per_page'] to wp_get_nav_menu_items $args, it returns the same amount of items as if the argument was left out.

It seems like this is due to the following line:

$args['include'] = $items;

The get_posts argument is described as following:

'include'
	(array) An array of post IDs to retrieve, sticky posts will be included. Is an alias of $post__in in WP_Query. Default empty array.

But really it does the following:

if ( ! empty($r['include']) ) {
    $incposts = wp_parse_id_list( $r['include'] );
    $r['posts_per_page'] = count($incposts);  // only the number of posts included
    $r['post__in'] = $incposts;
}

Meaning that if include is passed posts_per_page will get ignored.

An alternative would be to use post__in instead of include in wp_get_nav_menu_items():
https://github.com/WordPress/WordPress/blob/36759db92ed78900a9d96c63b4e0ff0ae6313fe7/wp-includes/nav-menu.php#L625

Change History (1)

#1 @welcher
7 years ago

  • Description modified (diff)
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@Compute thanks for the report!

In this case, I think that this is expected behavior. $items is a list of IDs as returned by get_objects_in_term( $menu->term_id, 'nav_menu' ); Because we are using a list of post ID's, it makes sense IMO that we would only want the number of posts that are represented in that returned list.

We can't ask for more posts as the list is passed to the posts__in arg which will only retrieve those post ID's. Passing a higher number than that list contains to posts_per_page will not return more posts and if it did, what would they be?

If we want less, there are already filters in place such as wp_get_nav_menu_items.

I don't see a use-case here but if you have one, please add it below and we can re-open

Note: See TracTickets for help on using tickets.