Home > Mobile >  Insert wordpress post using wp_insert_post and attach the featured image
Insert wordpress post using wp_insert_post and attach the featured image

Time:12-19

I tried to insert a post using the wp_insert_post function in the functions.php file, the post successfully inserted, but not for the attachment for featured image.

Anyone can help on this, what's wrong with my code below:

$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title'");
if($post_if < 1){
    //coded
    $new_post = array(
        'post_title' => $title,
        'post_content' => $contents,
        'post_status' => 'publish',
        'post_author' => 1,
        'post_type' => 'post'
);

$post_id = wp_insert_post($new_post); 
$image = "https://fake.org/image.jpg";


$media = media_sideload_image($image, $post_id); //$post_id from wp_insert_post


// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => $post_id
    );

    // reference new image to set as featured
    $attachments = get_posts($args);

    if(isset($attachments) && is_array($attachments)){
        foreach($attachments as $attachment){
            // grab source of full size images (so no 300x150 nonsense in path)
            $image = wp_get_attachment_image_src($attachment->ID, 'full');
            // determine if in the $media image we created, the string of the URL exists
            if(strpos($media, $image[0]) !== false){
                // if so, we found our image. set it as thumbnail
                set_post_thumbnail($post_id, $attachment->ID);
                // only want one image
                break;
            }
        }
    }
}

I tried so many tutorials, I found over the web, nothing to work.

Please any one has experienced with this can share a solution.

Big Thanks

CodePudding user response:

You would need to set the "featured image" first and then try to query it. You tried to do the opposite. Also set the parent id in the wp_insert_attachment function not in the arguments.

So try this code:

$new_post = array(
    'post_title'   => $title,
    'post_content' => $contents,
    'post_status'  => 'publish',
    'post_author'  => 1,
    'post_type'    => 'post'
);

$post_id = wp_insert_post($new_post);

$image = "https://fake.org/image.jpg";

$attachment_file_type = wp_check_filetype(basename($image), null);

$wp_upload_dir = wp_upload_dir();

$attachment_args = array(
    'guid'           => $wp_upload_dir['url'] . '/' . basename($image),
    'post_title'     => preg_replace('/\.[^.] $/', '', basename($image)),
    'post_mime_type' => $attachment_file_type['type']
);

$attachment_id = wp_insert_attachment($attachment_args, $image, $post_id);

require_once(ABSPATH . 'wp-admin/includes/image.php');

$attachment_meta_data = wp_generate_attachment_metadata($attachment_id, $image);

wp_update_attachment_metadata($attachment_id, $attachment_meta_data);

set_post_thumbnail($post_id, $attachment_id);

Here's the documentation page for

wp_insert_attachment

Reference:

https://developer.wordpress.org/reference/functions/wp_insert_attachment/#user-contributed-notes

  • Related