Home › Toolset Professional Support › [Resolved] Prepopulate Fields in Nested Repeatable Fields View
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Our next available supporter will start replying to tickets in about 0.49 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tagged: Views plugin
This topic contains 20 replies, has 2 voices.
Last updated by Minesh 5 years, 6 months ago.
Assisted by: Minesh.
Prepopulate Fields in Nested Repeatable Fields View
Is there any documentation that you are following? Nope
Have some pictures!
I've got a view of a nesting group of repeatable fields. i have to fill it in w every post. how do prepopulate those "day of the week" fields so i don't have to do it every time? my arm is asleep.
Using Genesis theme with custom child. Pls remove my website url from this page, thanks!
Hello. Thank you for contacting the Toolset support.
Well - can you please tell me what field you want to pre-populate. Do you mean that you wanted to add the default value for custom fields added to repeating field groups?
Hi, Minesh! I have 3 fields - the day of the week, opening time, and closing time. day(s) of the week will always be the same, so i want that list of fields to be populated. That way, I need only fill in the open and close times, which are, of course, different for different restaurants. Thank you!
Do you mean that you want to pre-populate the 7 days (mon-sun) one by one with 7 separate entries where each entry belongs to a specific day (sun, mon, tue...) and you just need to set the opening and closing time?
Yep.
Well - thats not possible because each repeating field group entry is a post (actually post in relation with the current post you are editing) so there is no way to create those 7 entries per day automatically until you add some custom programming.
Unfortunately, there is no native way to do this as there is no such feature.
Oh, no. I hate to get rid of my new toy. Can you suggest a better way to do this? I've got ACF and I'm reading up on it now.
What do you think of this?
/** * Preset a custom field for new posts * * @link http://wordpress.stackexchange.com/a/200554/26350 */ add_action( 'save_post_post', function( $post_ID ) { if( 'auto-draft' === get_post_status( $post_ID ) && post_type_supports( get_post_type( $post_ID ), 'custom-fields' ) ) add_post_meta( $post_ID, 'wpse_custom_field', '123' ); } );
Can you please share one test post link where I can check and access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Can you please check now. I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link
function func_add_rfg_entries( $post_id, $post,$update ){ $allowed_post_types = array("restaurant"); if ( in_array($post->post_type,$allowed_post_types)) { remove_action( 'save_post', 'func_add_rfg_entries',30,3 ); if($post->post_status != "auto-draft") { $child_posts = toolset_get_related_posts($post_id,'operating-hours','parent',999,0,array(),'post_id','child'); if(count($child_posts) == 0 ) { $field_name = "days-of-the-week"; // replace field slug which contains all 30 options $x = get_option('wpcf-fields'); $days_array = $x[$field_name]['data']['options']; unset($days_array['default']); foreach($days_array as $option): $my_post = array( 'post_title' => "rfg-entry-".$post_id."-".$option['value'], 'post_content' => '', 'post_status' => 'publish', 'post_author' => $post_id, 'post_type' => 'operating-hours'); // Insert the post into the database $insert_post_id = wp_insert_post( $my_post ); // addding the meta key add_post_meta($insert_post_id,'wpcf-days-of-the-week', $option['value']); /// establish the relationship beween current post and RFG entry toolset_connect_posts('operating-hours',$post_id, $insert_post_id); endforeach; } } add_action( 'save_post', 'func_add_rfg_entries',30,3 ); } } add_action( 'save_post', 'func_add_rfg_entries', 30, 3 );
Now, try to create a new post for your post type and you will see 8 entries for repeating field group will be automatically created.
More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/
Wow, thank you SO MUCH for this, Minesh! You have saved my arm from my (extant) Carpal Tunnel Syndrome! You're the best. Thanks again.
(Would you please remove my site url from the blue box at the top of this page? Thanks so much.)
Well - do not worry as that link will be shown to only logged in users that means you and me. I do not have any control to remove such information. You should check after logged out and see if you can still see the site url.
Please feel free to resolve the ticket 🙂 Gald to help and happy that solution I shared help you a lot to resolve your issue.
Ok, that's good to know about the blue box. I have a problem - the code screwed up the layout in the admin, and all over my site. I deleted it from toolset > settings > custom code, but the problem persists. Is there a way I can clear that cache? Thanks.
Well - thats another issue and there is nothing this code should affect your layout. It could be something other.
I tested yesterday and it used to work normal (in admin), once you create new post, it will automatically populated with those 7 days repeating field group entries.
Please open a new ticket for your every new question, this will help other users searching on the forum as well as help us to write correct problem resolution summery for which the current ticket opened.
(You can assign the new ticket to me)