0

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 );
}
6
  • You mean to set the featured image?
    – Tom J Nowell
    Jun 18, 2016 at 3:32
  • @TomJNowell Yes, exactly !
    – Picki
    Jun 18, 2016 at 3:48
  • Would this help?
    – cjbj
    Jun 18, 2016 at 8:37
  • Featured images are posts of type attachment, aka what you see listed in the media section of the admin area. Each post has a custom field/post meta which contains the post ID of the featured image. You will want your images as attachments so that they can be used by WordPress, rather than arbitrary files in a folder. Hopefully that information will be useful, and forms the foundation behind any answers that will be here
    – Tom J Nowell
    Jun 18, 2016 at 13:23
  • I'd recommend that you update your question to show the code that creates the posts and figures out the path for the image. The answer will involve sideloading the image, probably using media_handle_sideload
    – Tom J Nowell
    Jun 18, 2016 at 13:26

1 Answer 1

2

I 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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.