[Closed] Create new posts from template posts

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.

This topic contains 6 replies, has 3 voices.

Last updated by Christian Cox 1 month ago.

Assigned support staff: Christian Cox.

Author
Posts
#1632065

Hello! I'm creating a training plan website for runners. I had a long conversation with support about it here for background: https://toolset.com/forums/topic/wordpress-site-training-plans-for-runners/

I've got a lot of my custom posts types set up now. A training plan for a person looks like this:

Their Training Plan (Macrocycle)
- The first season of their plan (Mesocycle)
-- The first week of the first season (Microcycle)
---- The exercises to do in that first week (Sessions)
-- The second week of the first season (Microcycle)... etc.etc

I've build a plugin which uses the relationship hook to persist a permission allowing only individual users to look at each of these items ans they are created. That works well.

Sessions have some custom fields under them (Session Aim, and Notes) and a repeating field group where you put the details. I.e.
1 - Warm Up - Jog for 10 minutes at an easy pace
2 - Intervals - Run at max pace for 2 mins, then recover for 20 seconds. Repeat 10 times
3 - Cool Down - Job for 10 minutes at an easy pace

Here is an example I've made public: hidden link

I want to be able to create multiple sessions easily. Many of these will be very similar and just require customisation for the athlete I.e. most sessions will have a warm up and cool down. "Interval" sessions will have similar run fast/recover sections.

I'd like to be able to create a bunch of these and allow from the administration pages the ability for the admin to "copy" them into sessions in some way.

Are they any tools/code hooks or suggestions for how I might go about doing this? not scared to add custom code for it, but just need a clue where to start 🙂

#1632497

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I'd like to be able to create a bunch of these and allow from the administration pages the ability for the admin to "copy" them into sessions in some way.
Hi, let me be sure I understand the terminology and request here. You would like to be able to create individual line items for an RFG and copy them into multiple session posts, correct? Using the example you shared, you would like to create an RFG line item like:
1 - Warm Up - Jog for 15 minutes at an easy pace
This is slightly different from the existing Warm Up line item, so you would like to create this new line item and you would like to be able to copy this new line item easily into multiple Session posts from some type of interface in wp-admin. Did I understand that correctly?

If so, there's nothing exactly like this built-in to Toolset because RFG line items are not intended to be shared across multiple posts. There is no editor page for each RFG line item, for example, where you can edit the item and choose individual parent posts for assignment of this line item. In the parent post screen, there's no way to import a reusable RFG line item, so you can see the infrastructure, so to say, for this doesn't exist.

Other items designed to be shared across multiple posts are other posts in a many-to-many (M2M) relationship, taxonomy terms, and reusable blocks in the block editor. There are interfaces built-in to wp-admin that help facilitate sharing those things, but nothing built-in that will help facilitate sharing RFG line items. So that's the main challenge to overcome here, and there are no public Toolset APIs for building custom interfaces in wp-admin. That's a serious problem, so admin interface development is typically not supported here in the forums.

Is there a specific reason you decided to use RFGs here instead of a separate custom post type? I can see visually how it looks like a repeatable field group is the way to go, but there are some technical reasons why I would choose the M2M approach. My gut feeling here is you will be better off using a separate post type instead of an RFG here, and use a M2M relationship to allow you to associate these line items with one or more Sessions. This process in wp-admin is quick, and requires little or no custom code. You can create the line items in the standard post editor screen and manage related Session posts from that same edit screen, or you can edit the Session post and manage connected line items (add new ones, edit existing ones, delete existing ones) right there from the Session post edit screen.

If you decide you really need to use RFGs, it might be possible to create a new RFG line item and copy it to multiple posts using a customized Form on the front-end of the site. I could help you implement something like this using our Forms and Relationships APIs. You could use Access to hide the administration Form pages from other Users and only allow admin-level access on the front-end of the site. Note that these cloned line items will not remain linked to one another in any way, so editing the line item in one place will not edit the same line item globally. If you want something like that, another post in an M2M relationship is the way to go.

In the customized Form, you would remove the built-in parent post select field. This is where you would normally choose the parent post that would contain the newly created RFG item. Instead of the built-in select field, you will create a generic checkboxes field. Each possible Session post should be listed with a corresponding checkbox in the Form, and you will use these checkboxes to select into which posts you would like to copy the new RFG. You can use a View of Sessions posts to create the checkboxes options dynamically - I can show some examples of this in other forum tickets if you'd like some guidance on this. So you will create the RFG line item as usual in the Form, but select multiple parent posts to hold the new RFG, using checkboxes.

Along with the custom Form, you'll use our Forms API cred_save_data to hook into the Form submission event. In that hook, you'll write custom code that finds the submitted checkboxes in the $_POST superglobal, and loops over the checked checkboxes. Inside that loop, you'll use the newly created RFG line item (in the first iteration of the loop), or create a cloned RFG line item programmatically (in all other iterations of the loop). You'll use the toolset_connect_posts API to create a relationship between the checked checkbox parent post and the RFG line item or cloned RFG line item.

That's the general idea. It's a bit complex and will take a considerable amount of time and custom code to set up. Maintenance won't be easy - you'll have to edit each instance of the RFG individually in all the parent posts.Another limitation is usability - depending on how many Sessions you end up having on the site, selecting parent posts in checkboxes may become tedious. In the standard parent post select field, you'll have the ability to use a typeahead / autosuggest field to type and find the parent post easily. Similarly when you choose a related post from the post edit screen in wp-admin, you'll have an autosuggest field. However, this type of input is not available as a generic field in front-end Forms so you must manually find and check each individual checkbox.

Hopefully I've given you enough detail to help you understand the pros and cons of the various approaches here. Let's discuss your thoughts and go from there. Thanks!

#1634789

Hi, let me be sure I understand the terminology and request here. You would like to be able to create individual line items for an RFG and copy them into multiple session posts, correct? Using the example you shared, you would like to create an RFG line item like:
1 - Warm Up - Jog for 15 minutes at an easy pace
This is slightly different from the existing Warm Up line item, so you would like to create this new line item and you would like to be able to copy this new line item easily into multiple Session posts from some type of interface in wp-admin. Did I understand that correctly?

Not quite.

The trainer would be copying the whole session which contains some 2-3 fields and repeating fields. When you set up a plan, you will be creating 4-5 sessions a week, and having 10 weeks or more, and many of them will be repeats for that athlete, or even across athletes. You'd copy the barebones and then tweak the the details (i.e. update default text, add some speeds or times specific to the athlete).

I've opened up access to all the parts of a plan so you can get your head around how they are structured. I think it's probably worth making sure I'm not over doing this! 🙂

Here is a training plan (macrocycle) I've created for someone:
hidden link

Here you can see the aim of the plan and the structures beneath it. It has two mesocycles beneath it, a Spring Plan and Summer plan,

Spring Plan (Mesocycle): hidden link

Clicking into the Spring plan you can see it has a bunch of weeks (microcycles) underneath it, named after each week. On the first week, I've created some sessions.

Don't bother clicking into the microcycle / week 1. There isn't a template as I'm struggling to work out how to display it how I want. (that will be a question for later)

You can click into all the different sessions I've set up.

Looking at this Session: hidden link

Part of this plan is about improving arm drive for the athlete and this session is all about that. It might be put into every week of the plan and I want to way to do this without having to recreate it over and over again and create hassle for the trainer.

I hope all that makes sense!

So, before we go back to that orignal question, does this feel like I'm using the structures right? Or have I made this far to complicated (for what might be quite a complicated thing...) I never choose easy projects....

#1637853

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kelly,

Christian is currently unavailable today but he will be back tomorrow to continue this thread with you.

Thanks,
Shane

#1640283

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

So, before we go back to that orignal question, does this feel like I'm using the structures right? Or have I made this far to complicated (for what might be quite a complicated thing...)
The structure of the project does seem larger and a bit more complex than most simple Toolset projects, but not wildly so. The main issue I've already pointed out is that RFG instances are not easily shared across multiple posts. Instead, those should be converted to posts in some custom post type in a many-to-many relationship if you want to promote reusability.

Part of this plan is about improving arm drive for the athlete and this session is all about that. It might be put into every week of the plan and I want to way to do this without having to recreate it over and over again and create hassle for the trainer.
Cloning a Session into multiple Microcycles/weeks shouldn't be a huge problem because Sessions have no descendants. That means we don't have to worry about cloning all their descendants and applying the necessary post relationships, to all them. Sharing Micros across Mesos, or Mesos over Macros, will be understandably more complicated. You would have to query all the related posts and make sure they are all cloned as well, make sure they are set to the appropriate author, clone all their custom field values, and so on. It'll be a challenge.

#1645571

I think I only need to be able to "clone" sessions, rather than the other items. I want the other items to require some thought put into it by the trainer, where as cloning the session sis to get the trainer started 😀

I'm worried about the additional leg work required for the trainer to set up loads of posts for the session details bits. As it is right now the repeatable groups are simple for the trainer to set up (on the template or custom session) and are contained nicely in one place for them to update after they've cloned.

I guess I should poke around at the DB and see how hard it would be to clone them.

#1646795

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Repeatable field groups are stored as a custom post type that is in a one-to-many post relationship with their parent post. So if a Session contains a RFG, you must clone each item in that RFG and assign it to a new cloned Session in order to clone the Session and its RFGs. You can use the post relationship API to programmatically fetch all the RFGs associated with a Session. We have more information about that available here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
The relationship slug for connecting a parent post to its RFG is the same as the RFG slug. Post relationships details in the database are stored in proprietary tables.

The topic ‘[Closed] Create new posts from template posts’ is closed to new replies.