I have a custom Submenu page with my post type in which I am querying all posts and adding custom textareas and dropdowns that will repeat with each entry. The id and name for each field is the same accept the end of it is hyphenated with the post ID at the end. I want to be able to save the values entered in these fields and preferably once the data is entered instead of clicking a save button.
I figure that these texatareas and dropdowns need to be added into a group in order to save them, but I haven’t done it in this fashion before. I usually have the values in the individual post. But, I need this section separate from the actual posts in the post type.
Any suggestions would be helpful. Thanks in advance!!!
Here is what I have so far for this submenu page.
$args = array( 'post_type' => 'properties', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'DESC', 'post_status' => 'publish' );
$myposts = get_posts( $args );
?>
<div class="wrap">
<h1>Status:</h1>
<style>
.widefat .room-column {
width: 3.2em;
vertical-align: top;
}
.widefat textarea {
width: 100%;
}
</style>
<form action="" method="post">
<input type="submit" name="update_statuses" value="Update" class="button-primary" />
<table class="wp-list-table widefat fixed striped posts">
<thead>
<tr>
<th class="manage-column column-cb room-column">Beds</th>
<th class="manage-column column-columnname">Address</th>
<th class="manage-column column-columnname">Renewal</th>
<th class="manage-column column-columnname">Future Rent</th>
<th class="manage-column column-columnname">Availability Date</th>
<th class="manage-column column-columnname">Deposit</th>
<th class="manage-column column-columnname">Last Showing</th>
<th class="manage-column column-columnname">Status</th>
<th class="manage-column column-columnname">Date</th>
<th class="manage-column column-columnname">Initials</th>
<th class="manage-column column-columnname">Notes</th>
</tr>
</thead>
<?php
$count = 0;
foreach( $myposts as $post ) : setup_postdata($post);
$count++;
$bedrooms = wp_get_post_terms($post->ID, 'bedrooms', array("fields" => "all"));
foreach( $bedrooms as $room ) {
$bedSlug = $room->slug;
}
$bedStripped = preg_replace('/[^0-9]/', '', $bedSlug);
?>
<tr>
<th class="room-column">
<?php echo $bedStripped; ?>
</th>
<td>
<a href="https://wordpress.stackexchange.com/questions/346052/<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a>
</td>
<td>
<select name="renewal-<?php echo $post->ID; ?>" id="renewal-<?php echo $post->ID; ?>">
<option value="0">Choose</option>
<option value="No">No</option>
<option value="Yes">Yes</option>
</select>
</td>
<td>
<textarea name="future-rent-<?php echo $post->ID; ?>" id="future-rent-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<textarea name="available-<?php echo $post->ID; ?>" id="available-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<textarea name="deposit-<?php echo $post->ID; ?>" id="deposit-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<textarea name="last-showing-<?php echo $post->ID; ?>" id="last-showing-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<select name="status-<?php echo $post->ID; ?>" id="status-<?php echo $post->ID; ?>">
<option value="--">-</option>
<option value="nf">Needs Fees</option>
<option value="bgi">BGI</option>
<option value="fa">Final Approval</option>
<option value="a">Approved</option>
<option value="nd">Need Deposit</option>
<option value="dp">Deposit Paid</option>
<option value="ats">Appt to Sign</option>
<option value="rnl">Renew Lease</option>
<option value="ls">Lease Signed</option>
</select>
</td>
<td>
<textarea name="date-<?php echo $post->ID; ?>" id="date-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<textarea name="initials-<?php echo $post->ID; ?>" id="initials-<?php echo $post->ID; ?>"></textarea>
</td>
<td>
<textarea name="notes-<?php echo $post->ID; ?>" id="notes-<?php echo $post->ID; ?>"></textarea>
</td>
</tr>
<?php
endforeach;
echo '<div style="float:right;">Total: '.$count.'</div>';
?>
</table>
<input type="submit" name="update_statuses" value="Update" class="button-primary" />
</form>
</div>
2 Answers
If I understood you correctly, you’re basically asking how to group inputs by post ID? If this is the case, you could do something like this
// group by input
name="renewal[<?php echo $post->ID; ?>]"
// when saving
$_POST['renewal'] // array of input values with each having post_id as key
Or
// group by post ID
name="data[<?php echo $post->ID; ?>][renewal/future-rent/etc.]"
// when saving
$_POST['data'] // array of arrays, each with post_id as key. post_id specific subarrays have input names as keys and input values as values