I have referred to this link multiple image upload from frontend in wordpress for adding multiple images to a post. The code is working i.e., the images are uploading to media library but not showing in posts can you please let me know whether I have to create a field in Post to store them. If yes how?
Code for submitting a a post:
<div class="new-post">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title :</label><br />
<input class="form-control" type="text" id="title" value="" tabindex="1" size="20" name="title" minlength="5" required />
</div>
<div class="form-group">
<label for="description">Description :</label><br />
<textarea class="form-control" id="description" tabindex="3" name="description" cols="50" rows="6" minlength="30" required></textarea>
</div>
<div class="form-group">
<?php
$args_drop = array(
'show_option_none' => 'Category',
'hide_empty' => false,
'include' => '26, 27, 29, 34, 33',
'tab_index' => 3,
'taxonomy' => 'category',
);
wp_dropdown_categories($args_drop);
?>
</div>
<div class="form-group">
<label for="file">Image 2 :</label>
<input type="file" name="custom_1" value="Custom Field 1 Content" />
</div>
<div class="form-group">
<label for="postPhoto1">Photo 1 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto1">
</div>
<div class="form-group">
<label for="postPhoto1">Photo 2 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto2">
</div>
<input type="submit" value="Submit" tabindex="6" id="submit" name="submit" />
<input type="hidden" name="post_type" id="post_type" value="domande" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
Php for processing form :
if(is_user_logged_in() == true) {
// Check if the form was submitted
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
$custom_field_1 = $_POST['custom_1'];
// Add the content of the form to $post as an array
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => array($_POST['cat']),
'post_status' => 'publish', // Choose: publish, preview, future, etc.
'post_type' => 'work' // Use a custom post type if you want to
);
$pid = wp_insert_post($post); // Pass the value of $post to WordPress the insert function
if ( $_FILES ) {
$files = $_FILES['upload_attachment'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
}
}
}
$category_filter = array($_POST['cat_filter']);
}
Now the question is that through this code I am able to upload the images but where do I save them in the post. like a featured image has a section in post where it gets saved or how do I retrieve them to a page?
Thanks