Alternatively one could apply the general idea from @majick here and adjust the slug with some left padding.
Here's an example with left padding of length 5:
Note that if we used pure numeric like
00001 then it would be saved as
00001-2, to avoid collisions with default year routes, so that's why we prefixed it with some string, like
Then one could order by slug, i.e. the post_name column:
$query->set( 'orderby', [ 'name' => 'ASC' ] );
This doesn't need a join on the meta table, as if a custom field was used as an order field.
The user interface is already available to change the slug:
but this method becomes a problem if e.g. the number exceeds the padding length, if the user changes it and it might also be harder to maintain for large number of posts.
For a more dynamic way, one could also use the
posts_orderby filter and apply the various ideas from here, e.g. the
LPAD MySQL function.
Sorting in PHP
Taking the sorting to PHP, one could consider
usort() on the posts array, with the
strnatcmp() on the
post_title attribute, for natural (human) string comparison. This would be more suitable for few posts.