Prevent autosave on a custom post type in WordPress

Piggybank leaning on its nose on top of a pile of coins.
You get out what you put in. Nothing automatic about it.

I have a custom post type that doesn’t use the WordPress editor or its contents externally, but every time a post is saved or programmatically created, I do write contents to the post_content field so I can search for certain data in the WordPress back-end.

The problem is that every time a user goes to edit one of those posts, the autosave will overwrite my post_content data.

The following piece of code removes the autosave script when the user is editing a post of type movie.

add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');

function my_admin_enqueue_scripts() {
  switch(get_post_type()) {
    case 'movie':
      wp_dequeue_script('autosave');
      break;
  }
}

Featured image by Andre Taissin.