Skip Navigation

[Resolved] Predefined repeatable group

This support ticket is created 3 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 3 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#2173353
Screenshot 2021-09-20 at 12.00.30 AM.png
Screenshot 2021-09-20 at 12.00.03 AM.png

Dear Sir/Madam,

I refer to my discover site hidden link, is it possible to create some predefined repeatable group when I create a custom post (Tutor)?

I want to create a new tutor, can I create the predefined education like primary, secondary and university and of course user can create additional repeatable group. Could you introduce the script when I create a new tutor post?

Best regards,

Kelvin.

#2174301

Hello, I can show you how to create RFGs programmatically. You should be able to utilize this code to set up some automation for new Tutor posts and default RFG content. Here are a few things you should know about RFGs:
- Each row of an RFG is technically one post in a custom post type, so adding RFGs programmatically is accomplished by creating posts using wp_insert_post: https://developer.wordpress.org/reference/functions/wp_insert_post/
- The post type slug of the RFG is identical to the slug of the RFG. In your case, that slug is 'education', so when you create RFG rows you will be creating new 'education' posts. Do not create this custom post type in Toolset > Post Types. The system creates and manages this post type automatically in a private eway.
- After creating a new row of the RFG, that row must be connected to its parent Tutor post programmatically using Toolset's post relationships. Toolset uses a private one-to-many (O2M) post relationship to connect the parent Tutor (One) to each row of the RFG (Many). So you must connect each new row of the RFG with its parent Tutor post using the post relationships API toolset_connect_posts: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts. Do not attempt to create or manage this post relationship in Toolset > Relationships. The system creates and manages this relationship automatically in a private way.
- The toolset_connect_posts API requires you to provide the post relationship slug, the parent post ID, and the child post ID. The post relationship slug is the same as the RFG slug, so yours is 'education'. The parent post ID is the new Tutor post ID, and the child post ID is the ID of the new RFG post you created using wp_insert_post.

Can you answer these questions for me?
- Will Tutor posts be created only in wp-admin > Tutors > Add New, or do you plan to create Tutors using Toolset Forms?
- Are Tutor posts edited with the Block Editor, or with the classic editor? The solution might be different depending on the editor type.

#2174489

Dear Christian Cox,

Thanks for your advice.

The tutor will be created from wp-admin

Tutor posts will be edited with a classic editor.

I have questions about how I can

  • name the repeatable post like Primary, Secondary, ...
  • order the sequence
  • preset custom field content
#2175265

I have questions about how I can...name the repeatable post like Primary, Secondary, ...
The text like Primary, Secondary, etc. is set by the post title of the RFG post. So when you use wp_insert_post to create a row of the RFG, this text is set in post_title: https://developer.wordpress.org/reference/functions/wp_insert_post/#parameters

I have questions about how I can...order the sequence
The RFG sequence is set by a custom field toolset-post-sortorder applied to each RFG post. The first row of the RFG should have a toolset-post-sortorder value of 1. The second row should have a value of 2, the third row should have a value of 3, etc. Please note that you should NOT use the wpcf- prefix for the toolset-post-sortorder field. The full slug of this field is toolset-post-sortorder (see the screenshot of my postmeta table).

I have questions about how I can...preset custom field content
You set custom fields in an RFG just like you would set custom field values for any other post. You can use update_post_meta to set these field values programmatically. When you call wp_insert_post to insert an RFG, store the returned ID in a variable. Then use that variable as the ID of the RFG post when you call update_post_meta. When setting a Types field value, be sure to use the wpcf- slug prefix:

// $postarr should be an array of post information, for example:
// https://developer.wordpress.org/reference/functions/wp_insert_post/#comment-401 
$new_rfg = wp_insert_post( $postarr, $wp_error );
update_post_meta( $new_rfg, 'wpcf-fieldslug', 12345 );

Let me know what other questions you have.

#2176817

Dear Christian Cox,

Thanks for your reply. Sorry I am still not sure whether I should use wp_insert_post() to create the repeatable group, attached screenshot is my actual project to create course and then assign default materials inside, how can I insert the content? Could you explain the full code if I want to add the material including
Title (Text),
Stage (Select),
Order (Number),
Material Type (Select)
Google Form Link (URL) *
Opener (Select)

Please note the Google Form Link is depended on the Material Type select, Material Type can be Google Drive, Google From, PDF, Youtube, Website, because of different type of material, I will have different preview to the field.

Best regards,

Kelvin.

#2177695
Screen Shot 2021-09-23 at 12.14.05 PM.png

Sorry, I didn't receive any screenshot but I have an example that should help. Let's assume there is a custom post type Books (slug is "book") that includes an RFG Chapters (slug is "chapters). In the Chapters RFG there is a custom field Chapter Number (slug in wp-admin is "chapter-number", meta key in database is "wpcf-chapter-number"). There is a Form that creates new Book posts. When this Form is submitted, it should automatically generate 3 Chapter RFGs. It should automatically assign the correct sort order to the RFGs, and it should set the Chapter Number custom field to be 1, 2, and 3 respectively. See the screenshot here showing the new Book post and 3 automatically generated Chapters in wp-admin.

This code was used to generate the 3 Chapter RFGs shown here, in the proper order, with the custom field values defined:

// https://toolset.com/forums/topic/predefined-repeatable-group/
// Form 18 creates new book posts.
// This code automatically generates 3 chapter rfgs inside the new book,
// assigns the correct sort order, and sets chapter number custom field values in each RFG
// --
add_action('cred_submit_complete', 'tssupp_auto_generate_rfgs',10,2);
function tssupp_auto_generate_rfgs($post_id, $form_data)
{
  // trigger this code when Form 18 is submitted
  if ($form_data['id']==18)
  {
    // create chapter 1 rfg
    $chap1_args = array(
      'post_title'    => 'Chapter 1',
      'post_content'  => '',
      'post_type'     => 'chapters',
      'post_status'   => 'publish',
    );
    $chap1 = wp_insert_post($chap1_args);

    // add custom fields and set sort order
    update_post_meta($chap1, 'wpcf-chapter-number', 1);
    update_post_meta($chap1, 'toolset-post-sortorder', 1);

    // assign this RFG row to the new Book
    toolset_connect_posts( 'chapters', $post_id, $chap1);


    // create chapter 2 rfg
    $chap2_args = array(
      'post_title'    => 'Chapter 2',
      'post_content'  => '',
      'post_type'     => 'chapters',
      'post_status'   => 'publish',
    );
    $chap2 = wp_insert_post($chap2_args);

    // add custom fields and set sort order
    update_post_meta($chap2, 'wpcf-chapter-number', 2);
    update_post_meta($chap2, 'toolset-post-sortorder', 2);

    // assign this RFG row to the new Book
    toolset_connect_posts( 'chapters', $post_id, $chap2);


    // create chapter 3 rfg
    $chap3_args = array(
      'post_title'    => 'Chapter 3',
      'post_content'  => '',
      'post_type'     => 'chapters',
      'post_status'   => 'publish',
    );
    $chap3 = wp_insert_post($chap3_args);

    // add custom fields and set sort order
    update_post_meta($chap3, 'wpcf-chapter-number', 3);
    update_post_meta($chap3, 'toolset-post-sortorder', 3);

    // assign this RFG row to the new Book
    toolset_connect_posts( 'chapters', $post_id, $chap3);
  }
}

You now have examples showing how to create multiple RFGs, assign the proper sort order, and set custom field values in each RFG. Let me know if you need something else.