Skip Navigation

[Resolved] Record when changes are made to a certain field

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to check if a field value has changed when submitting a CRED that edits a Post
And if changed, update/add some actions.

Solution:
1. Use the cred_before_save_data action, as otherwise the past is already updated when you compare.

2, This is a example code, fully commented:

function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['id']==168) //CRED Form ID
    {
   
        //get existing post id
        $existing_post_id = $form_data['container_id']; //Current CONTAINER of Form (current Post)
        //https://toolset.com/documentation/user-guides/cred-api/#cbsd
  
        //get existing post field value
        $existing_post_status = get_post_meta($existing_post_id, 'wpcf-value', true); //Get post meta value of currnet post
   
        //get new field value
        $new_field_value = $_POST['wpcf-value']; //get value of current CRED Form field
   
            //If value has changed
            if ($existing_post_status != $new_field_value) //if this is not the same
            {
   
            //if, then insert the new post
            //values please refer to this DOC:
            //https://codex.wordpress.org/Function_Reference/wp_insert_post
            //I already put in a few examples, it's to adapt. Please read the WP Codex carefully for debug.
            $new_post = array(
            'post_content'   => '',
            'post_excerpt'   => '',
            'post_name'      => 'title',
            'post_status'    => 'publish',
            'post_title'     => 'title',
            'post_type'      => 'post',
            'menu_order'     => '');
 
        //insert post
        $new_post_id = wp_insert_post( $new_post );
   
             
  
            //NOW Update the new posts fields! required.
            //(it shoudl automatically create them if non-existent)
            update_post_meta($new_post_id, 'your-new-custom-field-slug', 1);
   
        }
    }
}
add_action('cred_before_save_data', 'record_status_change',10,1);

3. This checks the FORM's fields value against the CURRENT posts (same field) value.
The FORM must be on the POST that is edited currently

Relevant Documentation:
https://codex.wordpress.org/Function_Reference/wp_insert_post
https://codex.wordpress.org/Function_Reference/update_post_meta
https://toolset.com/documentation/user-guides/cred-api/#cbsd

This support ticket is created 8 years, 3 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 13 replies, has 2 voices.

Last updated by gavinS 8 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#358813

I have a type called 'CV Sent' which has a radio field called 'Status'.

I would like to record a datestamp when changes are made to the field. So I assume I need to create a new type with two fields, one to record the ID for which 'CV Sent' has changed. (What kind of field would this be? Numeric?) And another field to record the value of the changed radio field.

How do I automatically create new fields to record these changes when the edit form is submitted?

#358839

You can not create automatically fields with Toolset.

This requires custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

I would like to help you achieve it, but for that I need more informations.

1. Are you using CRED to perform the mentioned changes?
2. Do you want to store the Date stamp in the Same Post type in a given field, or in a complete different Post Type?

As a simple approach, use the CRED API and a CRED Form.
On submit of the Form you perform a custom PHP action which get_post_meta the given Date Field of the Edited Post, check against the given field's value in the Database of the same post, and then, if different, use update_post_meta to update another given field with the data.

You should use cred_before_save_data CRED API as this has to happen BEFORE the post is saved to the Database.
https://toolset.com/documentation/user-guides/cred-api/#cbsd

If you need to update another Post type with this info, you'll need to get_posts() the "other" post type and update a field in one of those posts.

Here is relevant Codex DOC:
https://codex.wordpress.org/Template_Tags/get_posts
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/update_post_meta

I would suggest to use a Date Custom Field, as this would then simply get a Timestamp and update with a Timestamp the new field.
Otherwise you will eventually need to convert Timestamp to date() and viceversa.

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#359164

Hi Beda

Thanks so much for all the info.

I am trying to learn PHP and would love to try and figure this out with your help if possible.

I am using CRED, and would like to store this in a separate type.

But I wish to create a new record every time the status is changed, not just update an existing record. I assume I will need to use wp_insert_post to do this? Am I right?

So I would assume that what I would do is get_posts the post that I am editing with the CRED Form, check if the status has changed, and if so, wp_insert_post a new record of the datestamp type? Am I on the right track?

I'm going to try and work on some code now and will send it to you if you can check for me..

Thanks

Gavin

#359172

Hi Beda

This is what I'm thinking, am I on the right track?

add_action('cred_before_save_data', 'record_status_change',10,1);
function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['id']==209)
    {
        $existing_post = ????; 
// How do I refer to the existing version of the current edited post in the CRED form?
        $existing_post_data = get_post($existing_post); 
        $existing_post_ID = $edited_post_data -> ID;
        $existing_post_status = get_post_meta($existing_post_ID, 'wpcf-cv-status', true);
          if ($existing_post_status != edited_post_cv_status    ????)  
                // How do I refer to the cv-status in the current edited post?
        {
            //create a new post of type 'status-change'
            $new_post = array(
                  'datestamp' = date(),  ???????  
                   'value' = edited_post_cv_status     ????
            );
     
            wp_insert_post ($new_post);
        }
    }
}
#359183

You should be able to access the container_id from $form_data
This container_id is the ID of the Page where you are working on.

But you should also be able to use $POST as in $_POST['my_custom_field']

The insert Post approach is correct if you need a new entry each change-

Also here, please await my reply tomorrow
I will try to craft a possibly straight forward sample for you to see how that who'll work.

I apologize that I can't provide it right now.

Thank you

#359462

Please keep the Edit Form on the SAME post/page as the content edited.
Otherwise this requires a resource hungry query and I can not provide it as it's full custom code.

So, with the CRED on the SAME post/page as the Post/page you edit, please try this code:

function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['container_id']==209)
    {

        //get existing post id
        $existing_post_id = $form_data['container_id']; 
        
        //get existing post field value
        $existing_post_status = get_post_meta($existing_post_id, 'wpcf-cv-status', true);

        //get new field value
        $new_field_value = $_POST['my_custom_field'];

            //If value has changed
            if ($existing_post_status != $new_field_value)  
            {

            //if, then insert the new post
            $new_post = array(
            //values please refer to this DOC:
            //https://codex.wordpress.org/Function_Reference/wp_insert_post
            );

            //insert post and VERY imporntat assing the $varaible of the Post ID for later Usage!
            $new_post_id = wp_insert_post( $new_post, $wp_error );

            $variable = whatever you need (timestmap?);
            //NOW Update the new posts fields! (it shoudl automatically create htem if non-existent)
            update_post_meta($new_post_id, 'your-new-custom-field-slug', $variable);

        }
    }
}
add_action('cred_before_save_data', 'record_status_change',10,1);
#360381

Hi Beda

Thanks for this.

So do I add values to the array ($new_post), and then insert, or just create an empty array? Would I need to update_post_meta() if I declared the values when creating the array, or could I just wp_insert_post?

All I want to store is the $new_field_value and the current date. Do I still need to give the post a title?

I want to store the date in whatever format Toolset stores date fields. Is this timestamp? Later, I'm going to want to filter the view by these dates. Do I then say $variable = date()?

Thanks

Gavin

#360404

Um.. Looking at this code, you seem to be using 'container_id' as two seperate things?

In this line:

// if a specific form
if ($form_data['container_id']==209)

you seem to be using 'container_id' as the FORM ID

But in this line:

//get existing post id
$existing_post_id = $form_data['container_id'];

You seem to be using 'container_id' as the POST ID?

Am I just confused? Which is it? The form id or the post id?

#360420

Ups, that is a bad one.

Sure, you want to check if it's the FORM and not the CONTAINER.

So, here is refactored code:

function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['id']==209) //CRED Form ID
    {
 
        //get existing post id
        $existing_post_id = $form_data['container_id']; //Current CONTAINER of Form (current Post)
        //https://toolset.com/documentation/user-guides/cred-api/#cbsd

        //get existing post field value
        $existing_post_status = get_post_meta($existing_post_id, 'wpcf-cv-status', true); //Get post meta value of currnet post
 
        //get new field value
        $new_field_value = $_POST['my_custom_field']; //get value of current CRED Form field
 
            //If value has changed
            if ($existing_post_status != $new_field_value) //if this is not the same
            {
 
            //if, then insert the new post
            //values please refer to this DOC:
            //https://codex.wordpress.org/Function_Reference/wp_insert_post
             $my_post = array(
              'post_title'    => 'My post',
              'post_content'  => 'This is my post.',
              'post_status'   => 'publish',
              'post_author'   => 1,
              'post_category' => array(8,39)
              //follow the DOC to add more or less arguments
            );

            //insert post and VERY imporntat assing the $varaible of the Post ID for later Usage!
            $new_post_id = wp_insert_post( $new_post, $wp_error );
 
            $variable = whatever you need (timestmap?); //AS example for timestamp date('U')
            //See: <em><u>hidden link</u></em>

            //NOW Update the new posts fields! required.
            //(it shoudl automatically create them if non-existent)
            update_post_meta($new_post_id, 'your-new-custom-field-slug', $variable);
 
        }
    }
}
add_action('cred_before_save_data', 'record_status_change',10,1);

Please do not hesitate to open a new thread if other issues or problems arise

Thank you for your patience.

#360423

Hi Beda

I'm not sure if you saw the previous post as I sent two in a row?

I only really want to save the new value and the timestamp. Do I need to give the post a title, status etc?

Can I not just include the custom fields when I create the array and then wp_insert_post, or do I need to insert the post and then update_post_meta to create the custom fields.

Also, I'm not sure what format to save the date as. I would like to use the same format that Toolset uses for date fields, as I want to use this field as a filter for future views. Would this be a timestamp / date('U')?

Thanks

Gavin

#360432

Please consult this DOC and my code comments in the previous reply carefully:
https://codex.wordpress.org/Function_Reference/wp_insert_post
https://toolset.com/forums/topic/record-when-changes-are-made-to-a-certain-field/#post-360420

- post_title and post_content are required

- post_status: If providing a post_status of 'future' you must specify the post_date in order for WordPress to know when to publish your post.
So best is set it to publish, so you're sure it's correct.

Setting less arguments won't save you time or resources, it just increases risk of wrong data storage.
The more details, the better, as you can read in the DOC, some args are required.

I suggest to store Timestamp values.
CRED Works with that, and it's numerical, which allows you to setup Views and comparisons with out any issues.
You can always return a normal Post Date with a Toolset ShortCode (wpv-post-date) as a timestamp too.

Custom fields need to be updated / inserted with the proper function and can't be insert in the wp_insert_post() function
https://codex.wordpress.org/Function_Reference/wp_insert_post
https://codex.wordpress.org/Function_Reference/update_post_meta

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#360656

Hi Beda

Thanks man. This is what I've tried, but I don't see any status changes in the database.

Any idea what I've done wrong?


add_action('cred_save_data', 'position_filled',10,2);

function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['id']==209) //CRED Form ID
    {
  
        //get existing post id
        $existing_post_id = $form_data['container_id']; //Current CONTAINER of Form (current Post)
        //https://toolset.com/documentation/user-guides/cred-api/#cbsd
 
        //get existing post field value
        $existing_post_status = get_post_meta($existing_post_id, 'wpcf-cv-status', true); //Get post meta value of currnet post
  
        //get new field value
        $new_field_value = $_POST['wpcf-cv-status']; //get value of current CRED Form field
  
            //If value has changed
            if ($existing_post_status != $new_field_value) //if this is not the same
            {
  
            //if, then insert the new post
            //values please refer to this DOC:
            //https://codex.wordpress.org/Function_Reference/wp_insert_post
             $my_post = array(
              'post_title'    => ('CV-status - '.date('U')),
              'post_content'  => ('Status change to '.$new_field_value),
              'post_status'   => 'publish'
                            //follow the DOC to add more or less arguments
            );
 
            //insert post and VERY imporntat assing the $varaible of the Post ID for later Usage!
            $new_post_id = wp_insert_post( $my_post, $wp_error );
  
            $current_date = date('U'); //AS example for timestamp date('U')
            //See: <em><u>hidden link</u></em>
            
 
            //NOW Update the new posts fields! required.
            //(it shoudl automatically create them if non-existent)
            update_post_meta($new_post_id, 'post_type', 'status-change');
            update_post_meta($new_post_id, 'wpcf-datestamp', $current_date);
            update_post_meta($new_post_id, 'wpcf-value', $new-field-value);
            update_post_meta($new_post_id, '_wpcf_belongs_cv-status_id', $existing_post_id);
  
        }
    }
}
add_action('cred_before_save_data', 'record_status_change',10,1);

#360705

This one is wrong:
update_post_meta($new_post_id, 'wpcf-value', $new-field-value);

But that should not have effect on the Post Insertion.

I must ask:

1. Does that Post type exist?
2. Can you test with a simple set of Title, content and status args? (no variables, to test)
3. The code I provided works very fine locally, if the given Form Field is not equal to the Field value stored in the actually edited Post.
4. The CRED Form MUST be inserted to the currently edited post.

This is the code again for reference, I can not debug WordPress and PHP Custom code, as this is working very fine locally and we use it on other sites too.

It seems that you need custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

function record_status_change($form_data)
{
    // if a specific form
    if ($form_data['id']==168) //CRED Form ID
    {
  
        //get existing post id
        $existing_post_id = $form_data['container_id']; //Current CONTAINER of Form (current Post)
        //https://toolset.com/documentation/user-guides/cred-api/#cbsd
 
        //get existing post field value
        $existing_post_status = get_post_meta($existing_post_id, 'wpcf-value', true); //Get post meta value of currnet post
  
        //get new field value
        $new_field_value = $_POST['wpcf-value']; //get value of current CRED Form field
  
            //If value has changed
            if ($existing_post_status != $new_field_value) //if this is not the same
            {
  
            //if, then insert the new post
            //values please refer to this DOC:
            //https://codex.wordpress.org/Function_Reference/wp_insert_post
            //I already put in a few examples, it's to adapt. Please read the WP Codex carefully for debug.
            $new_post = array(
            'post_content'   => '',
            'post_excerpt'   => '',
            'post_name'      => 'title',
            'post_status'    => 'publish',
            'post_title'     => 'title',
            'post_type'      => 'post',
            'menu_order'     => '');

        //insert post
        $new_post_id = wp_insert_post( $new_post );
  
            
 
            //NOW Update the new posts fields! required.
            //(it shoudl automatically create them if non-existent)
            update_post_meta($new_post_id, 'your-new-custom-field-slug', 1);
  
        }
    }
}
add_action('cred_before_save_data', 'record_status_change',10,1);

Thank you

#360974

Ha! Found it!

I needed to include the post-type in the original array, not bring it in with update_post_meta().

Thanks for all your help with my many threads. I've learnt a lot this week.

Gavin

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.