Skip Navigation

[Resolved] Create versioning of the custom post type

This support ticket is created 3 years, 5 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 11 replies, has 2 voices.

Last updated by Shane 3 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#2090453

Tell us what you are trying to do?
I have 5 custom post types and they have relationships between them.
I need to have record history of any modification it's made to the post type and their relationships.
There is a way to handle this?

best regards

#2090881

Shane
Supporter

Languages: English (English )

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

Hi Patrizio,

Thank you for getting in touch.

Unfortunately the record keeping would need to be implemented completely by custom code. While wordpress core itself does keep a revision of the posts, it doesn't keep any Meta data. This include custom fields and the post relationship information.

For any records to be kept you will need to write some custom code to record these.

Thanks,
Shane

#2091307

Hi Shane,
thank you very much for your answer.

Ok then if i make a new custom post type, where i want to create the history by manual save, how can i copy the data from one table to another?

Let me explain better, i have this 5 post type, and in a view i show all the information with relationship, and i want to make a "save" button, that will create a record situation of this information to a new table "post type history". There is a way to pass the fields to the new table?

THX

#2091643

Shane
Supporter

Languages: English (English )

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

Hi Patrizio,

Here we are talking about 2 different things a custom Post type and a Database Table.

Hopefully i'm correctly assuming that when you say "Pass field to a new table" you are referring to passing the values to this new post type.

If so then you should be able to. Where are you creating the posts on the frontend or the backend ? The main issue is that relationship fields are a little special so in order to create a history for this I would recommend storing the value i.e the ID of the field in a single line field, however this can easily become quite complex.

Achieving this can become quite cumbersome and requires a good amount of PHP knowledge to manipulate the data to programatically create the post and populate that newly created post with the field values from the currently viewed post. Also there will need to be some form of history management.

The reason is that your new post type can quickly become over a thousand posts given each time one post is updated an entry will be created. So if a use hits the update but 5 times then 5 history posts will be created.

Thanks,
Shane

#2091759

Hi Shane,
yes i mean pass the values to the new post type. I'm creating the posts on frontend. Yes, i was thinking to store the values on the new post type, not creating new relationships. The update will be saved manually, i mean when someone makes changes to the post type, then he has to press a button "save". For solve the problem of hits many time the "save", it's to check last input, if no variations to the filed, then no new post created.

How can handle all this with toolset?

Thx a lot

#2091887

Shane
Supporter

Languages: English (English )

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

Hi

I understand a bit better now. We do have a hook that you can use to intercept and create the new post on our Form completion.

For the editing to checking if the value has changed then you can use this hook below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

Before the update to the post is saved to the database you can check if a value has changed, this is going to be a bit tricky given that you will need to check the post against its existing values, this means checking each custom field value against its stored value in the database.

However this would should be suitable enough for you to perform your action.

You should also be looking at these functions below as well.
https://developer.wordpress.org/reference/functions/wp_insert_post/

https://developer.wordpress.org/reference/functions/update_post_meta/

Please let me know if this helps.
Thanks,
Shane

#2092317

Hi Shane,
thx for you answer but i don't need to intercept on form completion. My user will modify the fields he needs, and this could be more relationships and then he save the fields situation to the new post type. With this hook #Cred_before_save_data i have to perform on forms update, i'm right? In this case will create a new post type "history" for every single post type. I need a post type "history" that collect in once all the 5 post types value fields!

Have other suggestions?

#2092567

Shane
Supporter

Languages: English (English )

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

Hello,

With this hook #Cred_before_save_data i have to perform on forms update, i'm right? In this case will create a new post type "history" for every single post type.

You can use the Cred_before_save_data hook to compare the current value being sent and the previous value to see if they are the same and then create a post in the History post type. This is the way I see of doing it.

I need a post type "history" that collect in once all the 5 post types value fields!

I understand the need of the History post type as this is where you will be storing the previous version of the updated post, however i'm not sure what is meant by "all the 5 post types value fields".

Is it that you have 5 different post types, so you're thinking of storing the history of the 5 different post types in 5 different history post types?

Please let me know.

Thanks,
Shane

#2094257
schema-01.jpg

Hello,
i have attached a scheme, so you could better understand how is setup my custom post types.

So how can i get the value fields and insert on the new post type "history"?

Thx a lot

#2094591

Shane
Supporter

Languages: English (English )

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

Hi Patrizio,

Based on your diagram, you will be pulling relationship data to set in the history.

What you can do is to create single line custom fields for each of the Relationship in the History works post type.

What i mean by this is that a custom field is to be created for Mens, Vehicles, Radios, Tools. Then when a work is updated, you will pull the current relationship data from that Work post type and create a History Works post based on the current data of the post.

You can either store the ID of the relationships or you can store the Post Titles.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This should allow you to get the information from the post relationship.

Thanks,
Shane

#2094647

Hello Shane,

"What i mean by this is that a custom field is to be created for Mens, Vehicles, Radios, Tools. Then when a work is updated, you will pull the current relationship data from that Work post type and create a History Works post based on the current data of the post."

How can i achieve this??? I mean, create the custom fields on "history custom post": no problem! How can i get the values for then insert in the history post?

Thanks

#2094699

Shane
Supporter

Languages: English (English )

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

Hi Patrizio,

As mentioned the posts are related to the WORK post type. I'm assuming the history is only created when the Works post type is modified. If such is the case then you can use this function below.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This will allow you to get the related posts for the WORKS post type.

The function will need to be used with the combination of our Forms hook below.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

The toolset_get_related_post function essentially returns the post id of the related post. This function is to be used with the relationships that are one to one.

In the case of your Many to Many relationship the code below will need to be used.
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post_types

While coding this for you is out of the scope of our support forum. The most I can do is to point you in the direction of the functions that you will need to use to get this working.

Thanks,
Shane