I don’t even know where to start with figuring this out….
This function for functions.php grabs the first attached image in a post and sets it as the featured image. The problem is that it will not attach an image if it is already attached to another post. I’ve tested this a number of times.
How can this function be changed to allow an image that is already attached to another post be set as the featured image on a new post?
function wpse127196_auto_set_featured_image() {
global $post;
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children(
"post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('save_post', 'wpse127196_auto_set_featured_image');
add_action('draft_to_publish', 'wpse127196_auto_set_featured_image');
add_action('new_to_publish', 'wpse127196_auto_set_featured_image');
add_action('pending_to_publish', 'wpse127196_auto_set_featured_image');
add_action('future_to_publish', 'wpse127196_auto_set_featured_image');
First of all, I can’t understand why you need to hook 5 different actions:
save_post
is enough: it is called on post publish and update.After that the function you posted dont get the image in the post content, but take first image that are child of the current post.
An image is a child of a post when:
However relationship between posts and media is one to many: a post can have many media children, but a media can have only one parent.
This is the reason why if an image is already attached to a post (it is a child of the post) your function can’t work because that image can’t be child of 2 (or more) posts.
So, if you want to get first image in post content, you should look at post content and extract first image, then from the image take the image id, and finally use that id as post thumbnail.
As you can see, there are 2 functions used in the function above:
wpse127196_get_first_image
andwpse127196_get_img_id_from_url
.The first, that extract and image from the content, can be wrote in different way. Probably the most solid is to use an html parser (Google for it). A simpler but less affordable way is use a regex.
For sake of simplicity I’ll use regex here:
The function that retrieve the id starting from url, comes from here