rearrange the posts by published date in Menu posts selection

I need to rearrange the posts by ordering them from published date instead of alphabetical order in Menu posts selection panel, view all section.

I want them to order as same way in the most recent posts in the view all section as well.

Is it possible to achieve this via a hook or any other code ?

Topic menu-order menus Wordpress

Category Web


You can use this filter.

add_filter('nav_menu_meta_box_object', function($post_type) {
    if ($post_type && 'page' === $post_type->name) {
        // Give pages a higher priority.
        $post_type->_default_query['orderby'] = 'post_date';
        $post_type->_default_query['order'] = 'DESC';
        add_meta_box( "add-post-type-page", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', 'core', $post_type );
        return null;
    }
    return $post_type;
}, 10);

add_action( 'pre_get_posts', 'AS_order_posts_in_menu_admin' );
function AS_order_posts_in_menu_admin( $q ) {
  global $pagenow;
  if('nav-menus.php' !=$pagenow)
    return $q;

  if(isset($q->query_vars['post_type']) && $q->query_vars['post_type']== 'post'){
    $q->query_vars['orderby'] =  'date';
    $q->query_vars['order'] = "DESC";
  }
  return $q;
}

About

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