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 .

    Read More

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

Related posts

Leave a Reply

1 comment

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

    try:

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