I have a form in the frontend to submit post , I want to attach images uploaded via the form to the post.
this is my codes:
$post_sent = wp_insert_post(array(
'post_title' => esc_sql($_POST['title']),
'post_content' => wp_kses_post($_POST['content']),
'post_author' => $user_id,
'post_status' => 'pending',
‘post_type’ => ‘document’,
));
if ($_POST[‘_photo_id’]) {
foreach ($_POST[‘_photo_id’] as $file => $array) {
$filename = get_attached_file( $file );
$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’
);
wp_insert_attachment($attachment,esc_sql($file[‘file’]),$post_sent);
}