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?
1 Answer
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);