MySQL does not do “natural” sorting natively, though there are attempts to make it work.
I have no idea how that plugin you mention works but sorting by menu_order
with a post_title
fallback is trivial.
add_action(
'pre_get_posts',
function ($qry) {
if ($qry->is_main_query()) {
$qry->set('orderby','menu_order title');
}
}
);
Of course, that gets menu_order
equal 0 at the top of the sort, and I don’t think that is what you want.
Like MySQL, WP_Query
does not have a “natural sort” feature but if what you want is to order by menu_order
where that menu_order
is greater than 0, and otherwise by title, which is what I think you want (at at least on point in the question), you can do that with a filter.
add_action(
'posts_orderby',
function ($orderby,$post) {
global $wpdb;
return " IF({$wpdb->posts}.menu_order > 0, {$wpdb->posts}.menu_order {$wpdb->posts}.post_title, {$wpdb->posts}.post_title) ";
},
10,2
);
Barely tested. Possibly buggy. Caveat emptor. No refunds.
You could use a similar filter to try to implement a true natural sort but I don’t have time to write, test, and debug that.