In a custom post type, is there a way to include the “order” field that is available for pages?
Is there any built-in logic that prevents duplicate values in the “order” field?
The idea is to be able to sort a custom post type by a user-specified order, then alphabetically by a string-based custom field.
When declaring your custom post type using the register_post_type function, you have to add ‘page-attributes’ to the support field, like in the following example:
You’ll need to add any other supported meta boxes as well to the ‘supports’ field, see https://developer.wordpress.org/reference/functions/register_post_type/ for more information about the register_post_type fields.
Also as far as I know there isn’t any built in way to prevent two of the same order, this is because you can create sub-ordering based on heirarchy (so one group of children pages can have a different ordering than another)
In addition to @Dave-Hunt’s response, you can also add a filter, such as the following, to define a custom order – in this case, alphabetical by title. (Code thanks to Mark Leong’s blog post.) Remove the
is_admin()
check, if you want you custom order_by on the front-end as well.Update
For pre-save validation, see this answer: https://wordpress.stackexchange.com/a/40095/4645
where your options are discussed. Basically, it comes down to custom jQuery, as WordPress doesn’t have any pre-save hooks.
Also (duplicating my previous comment here for future reference), here’s how to expose the ‘menu order’ field in the admin, so it’s user-editable, as it is for pages: Adding ‘menu order’ column to custom post type admin screen