I have a CPT named homeslider. I have menu_order page-attribute enabled, how can I make the menu_order values unique so it won't have duplicates when assigning values to it?

  • You may add validation before post is updated!
    – Sumit
    May 9, 2016 at 12:19

1 Answer 1


You can interfere with save_post action then change menu_order to post_ID:

add_action('save_post', function($post_id, $post, $update) {
    if ($post->menu_order === $post_id) {
        return $post_id;
    } else {
        global $wpdb;

        $q = "UPDATE $wpdb->posts SET menu_order=%d WHERE ID=%d";

        $wpdb->query( $wpdb->prepare($q, [$post_id, $post_id]) );

        return $post_id;
}, 9, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.