admin管理员组文章数量:1021205
I have tried every solution from similar answers and nothing seems to work on Wordpress 5.0 +
When saving a post I want to set its featured image to the first image in the post content.
function auto_set_featured( $post_id, $post, $update ) {
$images = get_posts( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 1
) );
set_post_thumbnail( $post_id, $images[0]->ID );
}
add_action( 'save_post', 'auto_set_featured', 10, 3);
In set_post_thumbnail()
if I set the image id manually it works but it does not seem to pick up $images[0]->ID
I'm not sure why this doesn't work.
Note: I'm testing on posts which have multiple images in the content so $images
should be returning an array. I also tried using $post->ID
and get_the_ID()
in the query and it does not work. I also tried adding the post ID manually for post_parent
.
I have tried every solution from similar answers and nothing seems to work on Wordpress 5.0 +
When saving a post I want to set its featured image to the first image in the post content.
function auto_set_featured( $post_id, $post, $update ) {
$images = get_posts( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 1
) );
set_post_thumbnail( $post_id, $images[0]->ID );
}
add_action( 'save_post', 'auto_set_featured', 10, 3);
In set_post_thumbnail()
if I set the image id manually it works but it does not seem to pick up $images[0]->ID
I'm not sure why this doesn't work.
Note: I'm testing on posts which have multiple images in the content so $images
should be returning an array. I also tried using $post->ID
and get_the_ID()
in the query and it does not work. I also tried adding the post ID manually for post_parent
.
1 Answer
Reset to default 0Have you already used this image in a different post? The post_parent
may already be assigned to a different post ID from the one you are on. The code you have will only return images uploaded to this post first.
I've seen a few regex type solutions to this problem, however I think PHPs DOMDocument may be safer. This is also very experimental but seems to work for me:
function auto_set_featured($post_id, $post, $update ) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($post->post_content, LIBXML_NOWARNING);
libxml_clear_errors();
$images = $doc->getElementsByTagName('img');
if($images->length > 0) {
$first_image = $images->item(0)->getAttribute('src');
$attachment_id = attachment_url_to_postid($first_image);
if($attachment_id) {
set_post_thumbnail($post_id, $attachment_id);
}
}
}
Should be simple enough to follow, but it loads the post content, and looks for the first img
DOMElement. We then use that image URL with attachment_url_to_postid to get the attachment ID, which you pass to set_post_thumbnail as per your code.
I have tried every solution from similar answers and nothing seems to work on Wordpress 5.0 +
When saving a post I want to set its featured image to the first image in the post content.
function auto_set_featured( $post_id, $post, $update ) {
$images = get_posts( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 1
) );
set_post_thumbnail( $post_id, $images[0]->ID );
}
add_action( 'save_post', 'auto_set_featured', 10, 3);
In set_post_thumbnail()
if I set the image id manually it works but it does not seem to pick up $images[0]->ID
I'm not sure why this doesn't work.
Note: I'm testing on posts which have multiple images in the content so $images
should be returning an array. I also tried using $post->ID
and get_the_ID()
in the query and it does not work. I also tried adding the post ID manually for post_parent
.
I have tried every solution from similar answers and nothing seems to work on Wordpress 5.0 +
When saving a post I want to set its featured image to the first image in the post content.
function auto_set_featured( $post_id, $post, $update ) {
$images = get_posts( array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 1
) );
set_post_thumbnail( $post_id, $images[0]->ID );
}
add_action( 'save_post', 'auto_set_featured', 10, 3);
In set_post_thumbnail()
if I set the image id manually it works but it does not seem to pick up $images[0]->ID
I'm not sure why this doesn't work.
Note: I'm testing on posts which have multiple images in the content so $images
should be returning an array. I also tried using $post->ID
and get_the_ID()
in the query and it does not work. I also tried adding the post ID manually for post_parent
.
- what do you get? any errors? – Vishwa Commented Apr 26, 2019 at 8:49
-
do a
var_dump($images)
beforeset_post_thumbnail
and see what you get. – Alexander Holsgrove Commented Apr 26, 2019 at 8:50 -
@AlexanderHolsgrove it gives
array(0) { }
so i'm not sure why it is not returning any images. The query seems correct. – CyberJ Commented Apr 26, 2019 at 8:52
1 Answer
Reset to default 0Have you already used this image in a different post? The post_parent
may already be assigned to a different post ID from the one you are on. The code you have will only return images uploaded to this post first.
I've seen a few regex type solutions to this problem, however I think PHPs DOMDocument may be safer. This is also very experimental but seems to work for me:
function auto_set_featured($post_id, $post, $update ) {
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($post->post_content, LIBXML_NOWARNING);
libxml_clear_errors();
$images = $doc->getElementsByTagName('img');
if($images->length > 0) {
$first_image = $images->item(0)->getAttribute('src');
$attachment_id = attachment_url_to_postid($first_image);
if($attachment_id) {
set_post_thumbnail($post_id, $attachment_id);
}
}
}
Should be simple enough to follow, but it loads the post content, and looks for the first img
DOMElement. We then use that image URL with attachment_url_to_postid to get the attachment ID, which you pass to set_post_thumbnail as per your code.
本文标签: filtersWordPress set featured image to first image of the post
版权声明:本文标题:filters - WordPress set featured image to first image of the post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745549501a2155557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var_dump($images)
beforeset_post_thumbnail
and see what you get. – Alexander Holsgrove Commented Apr 26, 2019 at 8:50array(0) { }
so i'm not sure why it is not returning any images. The query seems correct. – CyberJ Commented Apr 26, 2019 at 8:52