Skip Navigation

[Resolved] cannot get parent/child post meta from cred_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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 1 year, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2479409
Screenshot 2022-10-20 at 12.35.14 AM.png

Dear Sir/Madam,

Below is a part of cred_form

	<div class="form-group">
		<label for="%%FORM_ID%%_@trainer-coupon.parent">[cred_i18n name='@trainer-coupon.parent-label']Trainer[/cred_i18n]</label>
		[cred_field field='@trainer-coupon.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='false']
	</div>
	<div class="form-group">
		<label for="%%FORM_ID%%_coupon-purchase-date">[cred_i18n name='coupon-purchase-date-label']Purchase Date[/cred_i18n]</label>
		[cred_field field='coupon-purchase-date' force_type='field' class='form-control' output='bootstrap' value="[today]"]
	</div>

I want to get the child or parent post meta to form the post title when cred_save_data hook, the relationship slug is trainer-coupon, I think the trainer is the parent, and the coupon is the child. Below please find my code, $form_id 257 is a form to create coupons. Please advise me why I can't get the value to variable $m or get the value of the field @trainer-coupon.parent

function ebz_cred_save_data($post_id,$form_data) {
    if ($form_data['id'] == 257) {
        
        $parent_posts = toolset_get_related_posts( $post_id, 'trainer-coupon', array( 'query_by_role' => 'child', 'return' => 'post_object'));
        foreach ($parent_posts as $parent_post) {
            $m = $parent_post->ID;
        }
        
        $title = sprintf("c%s - m%s", $post_id, $m);
        $args = array(
            'ID' => $post_id, 
            'post_title' => $title,
            'post_name' => $post_id,
            'meta_input' => array( "wpcf-coupon-last-update" => current_time( 'timestamp' ) )
        );
        wp_update_post($args);
    }
}
add_action('cred_save_data','ebz_cred_save_data',10,2);
#2479765

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL where you added the form and admin access details and tell me where you added the code. I'll have to review your current post relationship structure.

As I understand, you want to get the parent ID based on current child ID and update the parent post details - correct?

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

#2479959

Minesh
Supporter

Languages: English (English )

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

I just wanted to confirm what you want to display with the $title?

 $title = sprintf("c%s - m%s", $post_id, $m);

Do you want to display child post ID and parent post ID and coupon purchase quota?

#2480505

Dear Minesh,

First I am not sure which one is parent and child, for one-to-many relationship, should the one (member) must be parent? if yes, then I want to change the coupon title as c(the coupon post id) - m(member post id).

#2480517

Minesh
Supporter

Languages: English (English )

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

I see you have two one-to-many post relationship:
- Members (parent) << Coupons (child)
- Trainers (parent) << Coupons (child)

Toolset offers "Custom Code" section where you can add custom code:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

So I moved the "cred_save_data" hook to "Custom Code" section offered by Toolset:
=> hidden link

I've adjusted the code I've added to "Custom Code" section as given under:

function ebz_cred_save_data($post_id,$form_data) {
    if ($form_data['id'] == 257) {
        
        $parent_member_id = $_POST['@member-coupon_parent'];
      
        $title = $post_id."-".$parent_member_id;
        
        $args = array(
            'ID' => $post_id, 
            'post_title' => $title,
            'post_name' => $post_id,
            'meta_input' => array("wpcf-coupon-last-update" => current_time( 'timestamp' ) )
        );
        wp_update_post($args);
    }
}
add_action('cred_save_data','ebz_cred_save_data',10,2);

I can see now when I submit the form it saved the post title as you requested. Can you please confirm it works at your end as well:
=> hidden link

#2480667

Dear Minesh,

Is the custom code be stored in database, is there any easy way for me to export the code, I code in function because it looks like more easy to manage.

#2480675

Minesh
Supporter

Languages: English (English )

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

Yes, the custom code will be saved in database.

Ok, if you feel that its easy to manage the custom code in functions.php file its on you, I just shared that Toolset also offers the place where you can add custom code.

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