4

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 '16 at 12:19
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, privacy policy and cookie policy

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