How to make pages metabox display all hierarchy in menu admin?

The question:

Running WP 3.9.1

I’ve got lots of pages and sub pages.

When I get to create menu lots of sub pages doesn’t display on the right level, but some do?

This is how the pages metabox is displayed

The pages metabox should display something more like this


EDIT

Number in screenshot are a bit screwed sorry about that.

The problem is after the first few all the pages are displayed on the same level but they shouldn’t.

It should look like this

  • Page 1
    • SubPage A of Page 1
    • SubPage B of Page 1
      • SubPage 1 of SubPage B
      • SubPage 2 of SubPage B
      • SubPage 3 of SubPage B
  • Page 2
    • SubPage A of Page 2
      • SubPage 1 of SubPage A
      • SubPage 2 of SubPage A
    • SubPage B of Page 2
      • SubPage 1 of SubPage B
      • SubPage 2 of SubPage B


Any idea what cause this or how to fix it, so i can manage my menu?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

The previous answers propose editing core WordPress files, which is never a good idea, as new versions of WordPress will overwrite the changes (among other things).

While the original Trac ticket #18282 has been open since July 2011, the issue still exists in WordPress 4.6 — going on 5+ years now.

Fortunately there is a “safe” way of implementing a workaround to change the behavior without hacking WordPress core.

Sergey Biryukov, a core committer, wrote a small feature plugin that changes the Pages metabox behavior and preserves the hierarchy for both post types and taxonomies:

Preserve Page and Taxonomy Hierarchy on Edit Menus Screen

The plugin only affects the “View All” tab, not “Most Recent” or “Most Used”, but is a much more maintainable and responsible solution than patching core or hacking the files directly.

Method 2

Found a fix.

Seems pagination is breaking the display of the the Pages hierarchy in the metabox.

Here is the fix to disable page pagination in the nav menu metabox

https://core.trac.wordpress.org/attachment/ticket/18282/18282-disable-nav-menu-pagination.patch

Thanks

Method 3

This is my implementation of the solution mentioned above, updated for WordPress 4.6.1

In the file wp-admin/nav-menu.php edit the function wp_nav_menu_item_post_type_meta_box adding the argument ‘nopaging’ to the array starting at line 319 as follows:

$args = array(
    'offset' => $offset,
    'order' => 'ASC',
    'orderby' => 'title',
    'posts_per_page' => $per_page,
    'post_type' => $post_type_name,
    'suppress_filters' => true,
    'update_post_term_cache' => false,
    'update_post_meta_cache' => false,
    'nopaging' => true // add this argument
);

This will disable the pagination in the meta box and the pages hierarchy should show correctly, even if you have a long list of pages.

Hope it helps someone!

Method 4

Here’s a fix that you can apply in your own theme’s functions.php:

add_filter( 'nav_menu_meta_box_object', 'disable_pagination_in_menu_meta_box', 9 );

function disable_pagination_in_menu_meta_box($obj) {
  $obj->_default_query = array(
    'posts_per_page' => -1
  );
  return $obj;
}

Update: Needs to be added with priority 9 to work well with the default filter _wp_nav_menu_meta_box_object


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment