Skip to content

Instantly share code, notes, and snippets.

@seebeen
Created March 19, 2021 10:42
Show Gist options
  • Save seebeen/1ad42438922e08f4c441d153c40fc2ee to your computer and use it in GitHub Desktop.
Save seebeen/1ad42438922e08f4c441d153c40fc2ee to your computer and use it in GitHub Desktop.
WordPress add attachment to auto-save
<?php
add_action('save_post', 'add_attachment_to_autosave');
function add_attachment_to_autosave( $post )
{
if( !wp_is_post_autosave($post) ) :
return;
endif;
if ( get_post_meta($post->ID, '_thumbnail_id', true) ) :
return;
endif;
$attachment_id = 999;
add_post_meta($post->ID, '_thumbnail_id', $attachment_id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment