old_blueyes Posted July 31, 2016 Share Posted July 31, 2016 Hi, I am having a small issue with a front end html form, which through a series of looped input fields will update multiple WordPress posts at once, on submisson. The issue i'm having currently, is on submission it's only updating 1 post. I have found a solution to my problem, here: http://wordpress.stackexchange.com/questions/206372/how-to-handle-dynamic-form-data-with-repeating-fields But as i am still a bit of a novice, I have tried to apply the max_id solution to my code below, so far i've not got it to work. Using the solution on the above link, I don't suppose someone could show/tell me, what the code should be in it's entirety?? Including the foreach section, thanks! foreach( $testArray as $value ) { $post_information = array( 'post_title' => $value, 'post_status' => 'publish', // Choose: publish, preview, future, draft, etc. 'post_type' => 'predictions' //'post',page' or use a custom post type if you want to ); //update post wp_update_post($post_information); Quote Link to comment https://forums.phpfreaks.com/topic/301702-input-looping/ Share on other sites More sharing options...
Jacques1 Posted August 1, 2016 Share Posted August 1, 2016 (edited) Forget about the StackExchange thread, it's nonsense. PHP uses an array-like syntax to denote complex form values: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { var_dump($_POST); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Complex form values</title> </head> <body> <form method="post"> <input type="text" name="test[3]"> <input type="text" name="test[5]"> <input type="text" name="test[10]"> <input type="submit"> </form> </body> </html> When the above form is submitted, $_POST['test'] will be an array which maps the keys 3, 5 and 10 to the corresponding field values in the form. This allows you to easily update any number of posts. Simply use the post ID as the key and then iterate over the array: <?php function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_COMPAT | ENT_SUBSTITUTE, $encoding); } // test data; load the real post IDs here $post_ids = [12, 45, 69]; if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($_POST['post_title'] as $post_id => $post_title) { echo 'Post '.html_escape($post_id, 'UTF-8').' will get the title: '.html_escape($post_title, 'UTF-8').'<br>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Complex form values</title> </head> <body> <form method="post"> <?php foreach ($post_ids as $post_id): ?> <fieldset> <legend>Post <?= html_escape($post_id, 'UTF-8') ?></legend> <label>Title: <input type="text" name="post_title[<?= html_escape($post_id, 'UTF-8') ?>]"></label> </fieldset> <?php endforeach; ?> <input type="submit"> </form> </body> </html> Edited August 1, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301702-input-looping/#findComment-1535373 Share on other sites More sharing options...
old_blueyes Posted August 3, 2016 Author Share Posted August 3, 2016 Thanks very much Jacques!! I don't suppose you know how the wp_update_post() function would be implemented into your solution?? Thanks Forget about the StackExchange thread, it's nonsense. PHP uses an array-like syntax to denote complex form values: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { var_dump($_POST); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Complex form values</title> </head> <body> <form method="post"> <input type="text" name="test[3]"> <input type="text" name="test[5]"> <input type="text" name="test[10]"> <input type="submit"> </form> </body> </html> When the above form is submitted, $_POST['test'] will be an array which maps the keys 3, 5 and 10 to the corresponding field values in the form. This allows you to easily update any number of posts. Simply use the post ID as the key and then iterate over the array: <?php function html_escape($raw_input, $encoding) { return htmlspecialchars($raw_input, ENT_QUOTES | ENT_COMPAT | ENT_SUBSTITUTE, $encoding); } // test data; load the real post IDs here $post_ids = [12, 45, 69]; if ($_SERVER['REQUEST_METHOD'] == 'POST') { foreach ($_POST['post_title'] as $post_id => $post_title) { echo 'Post '.html_escape($post_id, 'UTF-8').' will get the title: '.html_escape($post_title, 'UTF-8').'<br>'; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Complex form values</title> </head> <body> <form method="post"> <?php foreach ($post_ids as $post_id): ?> <fieldset> <legend>Post <?= html_escape($post_id, 'UTF-8') ?></legend> <label>Title: <input type="text" name="post_title[<?= html_escape($post_id, 'UTF-8') ?>]"></label> </fieldset> <?php endforeach; ?> <input type="submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/301702-input-looping/#findComment-1535549 Share on other sites More sharing options...
Jacques1 Posted August 3, 2016 Share Posted August 3, 2016 (edited) You can literally copy and paste your code into my foreach loop. You have to add the 'ID' key to your array, though: WordPress Function Reference/wp_update_post Edited August 3, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/301702-input-looping/#findComment-1535553 Share on other sites More sharing options...
old_blueyes Posted August 3, 2016 Author Share Posted August 3, 2016 That's what I thought, i've had another go and it's now working, thank you for all your help!! You can literally copy and paste your code into my foreach loop. You have to add the 'ID' key to your array, though: WordPress Function Reference/wp_update_post Quote Link to comment https://forums.phpfreaks.com/topic/301702-input-looping/#findComment-1535554 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.