Thanks.
It worked.
However it turns out that its not returning what I need.
How do I check if there is a “featured image” attached to a post?
I’ve tried
global $post; global $wpdb;
$attached_file_check = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
if( get_post_meta($post->ID, "_videoembed", true) && !isset($attached_file_check) )
{
//function to attach existing videothumbnail goes here
}
It keeps firing the function leading to thousands of entries in my database to same thumbnails.
I am going to paste the entire function below, just in case you see other errors in my code.
function postmeta_img_fromvideo() {
global $post; global $wpdb;
$attached_file_check = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
//if(get_post_meta($post->ID, "_videoembed", true) AND get_post_meta($post->ID, "_thumbnail_id", true) != true)
if( get_post_meta($post->ID, "_videoembed", true) && !isset($attached_file_check) )
{
$videofilename = get_post_meta($post->ID, "_videoembed", true);
$vid_sub_ext = substr($videofilename, 0 , -4);
$thumb = $vid_sub_ext . '_1.jpg';
$thumbnail = str_replace('https://www.mydomainname.com/wp-content/', '', $thumb);
$wp_filetype = wp_check_filetype(basename($thumbnail), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => get_the_title(),
'post_content' => 'Image for '.get_the_title(),
'post_status' => 'inherit'
);
wp_insert_attachment($attachment, $thumbnail, $post->ID);
// Get Attachment ID for Post
$attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
// Attached Image as Featured Thumbnail in Post
update_post_meta($post->ID, "_thumbnail_id", $attachment_id);
}
}
add_action('the_post','postmeta_img_fromvideo');
Thanks again for your help. I’m trying to automate this stuff as much as possible.