I am creating posts using wp_insert_post()
, but these posts do not have featured images.
I have a post ID, and a path to the image on my server, how do I set them as the featured image?
EDIT : I added my code to publish an article. It only needs to add the thumbnail (featured image).
imagePath= ABSPATH . "wp-content\uploads\image\my_image.jpg";
publication($titre, $content, $categriesID, $tags);
function publication ($titre, $content, $categories, $tags){
$user_id = get_current_user_id();
$my_post = array(
'post_title' => $titre,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_category' => $categories,
'tags_input' => $tags
);
$post_ID = wp_insert_post( $my_post );
}
I am creating posts using wp_insert_post()
, but these posts do not have featured images.
I have a post ID, and a path to the image on my server, how do I set them as the featured image?
EDIT : I added my code to publish an article. It only needs to add the thumbnail (featured image).
imagePath= ABSPATH . "wp-content\uploads\image\my_image.jpg";
publication($titre, $content, $categriesID, $tags);
function publication ($titre, $content, $categories, $tags){
$user_id = get_current_user_id();
$my_post = array(
'post_title' => $titre,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => $user_id,
'post_category' => $categories,
'tags_input' => $tags
);
$post_ID = wp_insert_post( $my_post );
}
Share
Improve this question
edited Jun 27, 2020 at 20:51
Kalnode
2261 silver badge11 bronze badges
asked Jun 18, 2016 at 2:30
PickiPicki
1215 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 2I am proud to announce that I have found the solution thanks to @cjbj good advice. I paste the code here. I know it will help others.
In addition, I used the function preg_quote
to protect the path to the image.
protected_path = preg_quote($filename);
This code works great.
// $filename should be the path to a file in the upload directory.
$filename = '/path/to/uploads/2013/03/filename.jpg';
// The ID of the post this attachment is for.
$parent_post_id = 37;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $parent_post_id, $attach_id );
media_handle_sideload
– Tom J Nowell ♦ Commented Jun 18, 2016 at 13:26