forexfilms.com
Member
Posted 4 months ago #
I've come to a brick wall trying to set the featured image of a post with PHP. I'm using wp_insert_post() to make the post but I don't see any option to set the featured image. I also can't seem to find a function to set it.
What I specifically want to do was add a featured image from a url. The theme I'm using supports featured images and I'm currently manually entering them into posts.
forexfilms.com
Member
Posted 4 months ago #
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => $filename,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $thumbnail, $post_id );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $thumbnail );
wp_update_attachment_metadata( $attach_id, $attach_data );
// add featured image to post
add_post_meta($post_id, '_thumbnail_id', $attach_id);