Skip Navigation

[Resolved] Get new and previous value before save data

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

Problem:
Update the same Custom Field for All posts of a Custom post type with a certain value, when one cred edit post form is submitted from front-end.

Solution:
Please add this code in your theme’s or child theme’s functions.php file. Here I have done it for one Custom Field as example, you can copy the code to update it and add as many fields as you like:

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==3780)
    {
         
        if (isset($_POST['_cred_cred_prefix_post_id']))
        {
            $current_post_id = $_POST['_cred_cred_prefix_post_id']; // post id
            $field_old_value = get_post_meta( $current_post_id, 'wpcf-resolved-by', true ); // 'wpcf-resolved-by' Custom Field Slug
            $field_new_value = $_POST['wpcf-resolved-by']; // 'wpcf-resolved-by' Custom Field Slug
            echo 'Old Value '.$field_old_value ."<br>";
            echo 'New Value '.$field_new_value ."<br>";
             
             
            $args = array( 
                'post_type' => 'student', // CPT slug
                'posts_per_page' => -1, // All Posts
            );
             
            $the_query = new WP_Query( $args );
             
            // The Loop
            if ( $the_query->have_posts() ) :
                while ( $the_query->have_posts() ) : $the_query->the_post();
                  if( get_the_ID() == $current_post_id ) // Ignore Current Edit post 
                    continue;
                     
                    $key_1_value = update_post_meta( get_the_ID(), 'wpcf-resolved-by', $field_new_value ); // // 'wpcf-resolved-by' Custom Field Slug
                     
                endwhile;
            endif;
            // Reset Post Data
            wp_reset_postdata();
             
        }    }}

==> Please note the comments in the above code and replace your CRED form ID, Custom field slug, CPT slug, etc where needed.

Relevant Documentation:
API Doc: https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

This support ticket is created 6 years, 8 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by marcoR-6 6 years, 8 months ago.

Assisted by: Noman.

Author
Posts
#562689

Hi,
I need a help with CRED API. What I have to do, is that:

When I click the submit button in my cred, I need to do update the same field for all custom post type with a certain value.

------------------------------
EXAMPLE:
I have a custom post type called "test". This post type, contain some custom fields (FIELD1, FIELD2, FIELD3).
I have also create 10 post "test". So, if I change the value of FIELD1 in one of mine 10 post, I need to change FIELD1 for all other(9) posts with the FIELD3 set with a specific value.
------------------------------

I think it can be possible with a simple wp-query, and for each posts that return me the query, I can update FIELD1 value with "update_post_meta" function. (Tell me if I'm wrong).

But, before update the values I need to do some check, to set the new value for all other posts.

So, to do that, I need inside my hook, the previous value and the new value of the current post. How can I do that?

Thanks,
M.

#562761

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Marco,

Thank you for contacting Toolset support. Yes, you can do that with some custom code, using cred_before_save_data, update_post_meta and a loop. The below code will work for a CRED Edit form >> if you edit Post 1, Custom fields >> it will update custom fields in All other posts within the same CPT (using CRED form).

Please add this code in your theme’s or child theme’s functions.php file. Here I have done it for one Custom Field as example, you can copy the code to update it and add as many fields as you like:

add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
    // if a specific form
    if ($form_data['id']==3780)
    {
		
        if (isset($_POST['_cred_cred_prefix_post_id']))
        {
			$current_post_id = $_POST['_cred_cred_prefix_post_id']; // post id
			$field_old_value = get_post_meta( $current_post_id, 'wpcf-resolved-by', true ); // 'wpcf-resolved-by' Custom Field Slug
			$field_new_value = $_POST['wpcf-resolved-by']; // 'wpcf-resolved-by' Custom Field Slug
			echo 'Old Value '.$field_old_value ."<br>";
			echo 'New Value '.$field_new_value ."<br>";
			
			
			$args = array( 
				'post_type' => 'student', // CPT slug
				'posts_per_page' => -1, // All Posts
			);
			
			$the_query = new WP_Query( $args );
			
			// The Loop
			if ( $the_query->have_posts() ) :
				while ( $the_query->have_posts() ) : $the_query->the_post();
				  if( get_the_ID() == $current_post_id ) // Ignore Current Edit post 
				  	continue;
					
					$key_1_value = update_post_meta( get_the_ID(), 'wpcf-resolved-by', $field_new_value ); // // 'wpcf-resolved-by' Custom Field Slug
					
				endwhile;
			endif;
			// Reset Post Data
			wp_reset_postdata();
			
        }
    }
}


==> Please note the comments in the above code and replace your CRED form ID, Custom field slug, CPT slug, etc where needed.

API Doc: https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

I hope it helps, Thank you

#562812

It works! Thank you so much Noman!

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