Pit Digger
Pit Digger

Reputation: 9800

Adding Post With Featured Image Programmatically

I am adding posts dynamically to wordpress. I am using following code to create post and add a featured image which is working fine but there are 2 problems

  1. It adds 2 posts 1 with image itself no other content and correct with post with that image as featured image .
  2. Featured image in post size large .

    $post_id = wp_insert_post( $postdata, true );
    //Saving Image from a url
    $attachmentId_id = wp_insert_attachment( $attachment,$fullpathfilename,0);
    add_post_meta( $post_id, '_thumbnail_id', $attachmentId, true );

Upvotes: 3

Views: 3459

Answers (1)

shapeshifter
shapeshifter

Reputation: 2967

wp_insert_attachment creates a post itself if parent id is set to zero.

try:

wp_insert_attachment( $attachment,$fullpathfilename,$post_id);

Upvotes: 3

Related Questions