Skip Navigation

[Resolved] Find and change CPT field on form submission

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.

Author
Posts
#1397083

Hello

I have an edit user form with ID=2328. On the form is a single select option with either 'Yes' or 'No' (asking Do I have children?)

If the user selects 'No' then when the form is submitted I want a hook that performs these tasks:

1. Find all CPT 'People'(that have been created by the current logged in user) where the Field value of these people 'Child' = 'Yes'
2. Replace the field value of 'Child' with 'No' and save

I thank you in advance for your help and time with this..

Best regards

Geoff

#1397205

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Toolset Form's hook cred_save_data in order to perform such custom updates to table.

For example: - Please try to added the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

add_action('cred_save_data','func_update_child_field_values',10,2);
function func_update_child_field_values($post_id,$form_data) {

    global $current_user;

    if ($form_data['id']==2328) {
           
       $query_args = array(
	'post_type' => 'people',
	'post_status' => 'publish',
       'posts_per_page' => -1,
	'author' =>  $current_user->ID ,
	'meta_query' => array(
		  array(
			'key' => 'wpcf-child',
			'value' => 'Yes',
			'compare' => '=',
		) ));

// The Query
$people_ids = new WP_Query( $query_args );

if(!empty($people_ids )) {
foreach($people_ids  as $k=>$v):
  update_post_meta($v,'wpcf-child', 'No');
endforeach;
}

  }
}

Where:
- Please adjsut the above code as required for meta keys etc..etc..

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1397445

Hi Minesh...

Thanks for your help with this..

I have followed your instructions and the code is not working...The only things I changed were I added a ' that was missing after func_update_child_field_values,10,2) and I changed the CPT to 'Person' rather than the plural 'People'...is this the right thing to do?

I've added it to the custom code section but the function won't fire..

Many thanks as always

Best regards
Geoff

#1397461

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - can you please share problem URL where I can see the profile form and admin access details.

Also, please setup a user who already have few people posts with 'Child' field where value set to Yes and send me that user access details as well, so I ca use this user for testing.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1397623

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now. The issue why the code was not fired because the code snippet you added but not activated. I've activated it and adjust the code as required.

I've adjusted the code as given under:
=> hidden link

add_action('cred_save_data','func_update_child_field_values',10,2);
function func_update_child_field_values($post_id,$form_data) {
 
    global $current_user;
    
  if ($form_data['id']==2328) {
  
   // 2 == No child
    if($_POST['wpcf-do-you-have-any-children']==2) {
             $query_args = array(
    			'post_type' => 'person',
    			'post_status' => 'publish',
                         'fields'=> 'ids',
      			 'posts_per_page' => -1,
   				 'author' =>  $current_user->ID ,
   				 'meta_query' => array(
         					 array(
           						 'key' => 'wpcf-child',
           						 'value' => array('Yes','yes'),
           						 'compare' => 'IN',
        			) ));
 
// The Query
$people_ids = new WP_Query( $query_args );
$found_posts = $people_ids->posts;
   
    
         if(!empty($found_posts)) {
              foreach($found_posts  as $k=>$v):
                  update_post_meta($v,'wpcf-child', 'No');
              endforeach;
          }
 
  }
      
  }
  
}

I see now all those three entries you added now child field value is changed to No.

#1397627

My issue is resolved now. Thank you!