[wp-trac] [WordPress Trac] #41508: wp_get_nav_menu_items() does not account for posts_per_page

WordPress Trac noreply at wordpress.org
Tue Sep 19 02:33:12 UTC 2017


#41508: wp_get_nav_menu_items() does not account for posts_per_page
--------------------------+-----------------------
 Reporter:  Compute       |       Owner:
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:
Component:  Menus         |     Version:  4.8
 Severity:  normal        |  Resolution:  invalid
 Keywords:                |     Focuses:  template
--------------------------+-----------------------
Changes (by welcher):

 * status:  new => closed
 * resolution:   => invalid
 * milestone:  Awaiting Review =>


Old description:

> 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

New description:

 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

--

Comment:

 @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

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41508#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list