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 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.