Last active
September 30, 2021 14:10
-
-
Save mrkdevelopment/9bbc2950fe57cd9a675d33684f964a71 to your computer and use it in GitHub Desktop.
How to save a thumbnail ID and make sure the attached Post ID is referenced in Media Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('save_post', 'relate_post_to_thumb'); | |
function relate_post_to_thumb($post_id) { | |
//does this post have a URL / thumbnail | |
$thumb_id = get_post_thumbnail_id($post_id); | |
//if thumb found relate to post | |
if($thumb_id){ | |
//does our thumb have a post parent | |
if(!has_post_parent($thumb_id)){ | |
wp_update_post( | |
array( | |
'ID' => $thumb_id, | |
'post_parent' => $post_id | |
) | |
); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment