Orderby menu_order not working; menu is sorted on publish date

I have made a menu in the admin page ordered like this:

parent
    - child 1
    - child 2
    - child 3
parent
    - child 1
    - child 2
    - child 3
parent
    - child 1
    - child 2
    - child 3

When I load them in my navbar with the following code it works just fine:

wp_nav_menu( array(
    'theme_location' = 'menu-1',
    'menu_id'        = 'primary-menu',
    'menu_class'     = 'top-menu',
    ) );

However, I want to load a specific menu on a page based on it's parent. I have done this as followed:

?php 
$children = get_posts( array( 'post_parent' = $parent, 'post_type' = 'page', 'numberposts' = 9, 'orderby' = 'menu_order', 'order' = 'ASC')); 
?

But when it is displayed it displays the menu according to their publishing date:

- child 2
- child 1
- child 3

I know I can just change the publishing date but this may conflict in the future.

How do I order these children the same way as I have in the navbar?

Topic menu-order order children menus Wordpress

Category Web


menu_order doesn't mean the order in which it is used in any given custom menu. It means order by the "Order" attribute as entered in this box:

enter image description here

Counterintuitively this attribute is saved into the menu_order column in the wp_posts table in the database, which is why the orderby argument value is menu_order.

I'm not certain as I wasn't using WordPress at the time, but I believe this is a holdover from a time when WordPress did not have customisable menus and the only way to output a menu of pages was the wp_list_pages() function, which was introduced in 1.5.0 (wp_nav_menu() wasn't introduced until 3.0.0). When that was the way of doing menus ordering menu items was done with this attribute.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.