Skip Navigation

[Resolved] How to Get Child Post Fields to Inherit Values of Parent Post Fields by Default

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

Problem:
How to Get Child Post Fields to Inherit Values of Parent Post Fields by Default

Solution:
You need to use CRED hook cred_save_data to add/update values for your parent and child post type.

You can find proposed solution with the following reply:
=> https://toolset.com/forums/topic/how-to-get-child-post-fields-to-inherit-values-of-parent-post-fields-by-default/page/2/#post-581256

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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

Our next available supporter will start replying to tickets in about 1.34 hours from now. Thank you for your understanding.

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)

Tagged: 

This topic contains 22 replies, has 2 voices.

Last updated by jonB-5 7 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#581229

To confirm, if one of the repeating field values matches the current user ID the function should return a value of 'true'

Do please let me know how that would be implemented

#581235

Minesh
Supporter

Languages: English (English )

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

Well - what you are going to store with repeating field value - user ID?

as repeating field will be saved with postmeta table and it will have post_id, meta_key and meta_value.

#581237

Ah my apologies, I'm forgetting this is separate from the thread about setting up that field.

The repeating field values will be specific user ids, selected from a dynamically generated list.

If you go to the thread at https://toolset.com/forums/topic/dynamic-checkboxes-field-to-associate-users-with-custom-posts-created-in-cred/ you will see how these field values are generated.

I hope that helps, though do let me know if you need further clarification

#581241

Minesh
Supporter

Languages: English (English )

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

so the field that holds the user ID is "wpcf-vessel-authorised-reseller"?

If yes - then:

function func_check_authorize_user() {
	$current_user_id = get_current_user_id();
$user_ids = get_post_meta($post_id, 'wpcf-vessel-authorised-reseller');
  
        foreach($user_ids as $k=>$v):
             if($v==$current_user_id){
               return 1;
             }
        endforeach;
     
        return 0;
}
add_shortcode( 'check_authorize_user', 'func_check_authorize_user');

now, to use above shortcode with [wpv-conditional] shortcode - you need to register the "check_authorize_user" shortcode at:
=> Toolset => Settings => Frontend content tab => Third-party shortcode arguments section

now, you can use above shortcode as:

[wpv-conditional if="( '[check_authorize_user]' eq  '1')"]
user ID  found in repeating field with post meta 
[/wpv-conditional]
[wpv-conditional if="( '[check_authorize_user]' eq  '0')"]
user ID NOT found in repeating field with post meta 
[/wpv-conditional]
#581256

Great, this has worked - I just have one loose end to solve, as follows...

Currently the Authorised Resellers are selected through two CRED forms (New Vessel and Edit Vessel)

I have found that if I use the Edit Vessel form to un-select a Reseller, the field value is not removed from the Vessel CPT.

Can you advise on how to make this possible?

The current code is as below...

// New Vessel CRED Form - Save Authorised Resellers to CPT
add_action('cred_save_data', 'new_vessel_save_authorised_reseller',10,2);
function new_vessel_save_authorised_reseller($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==193)
        
    {
        $vals= isset( $_POST["vessel-authorised-reseller"] ) ? $_POST["vessel-authorised-reseller"] : array();
        foreach($vals as $val) {
          add_post_meta($post_id, 'wpcf-vessel-authorised-reseller', $val, false);
        }      
    }
}

// Edit Vessel CRED Form - Save Authorised Resellers to CPT
add_action('cred_save_data', 'edit_vessel_save_authorised_reseller',10,2);
function edit_vessel_save_authorised_reseller($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==208)
        
    {
        $vals= isset( $_POST["vessel-authorised-reseller"] ) ? $_POST["vessel-authorised-reseller"] : array();
        foreach($vals as $val) {
          add_post_meta($post_id, 'wpcf-vessel-authorised-reseller', $val, false);
        }      
    }
}

#581257

Minesh
Supporter

Languages: English (English )

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

Could you please try following code:

// Edit Vessel CRED Form - Save Authorised Resellers to CPT
add_action('cred_save_data', 'edit_vessel_save_authorised_reseller',10,2);
function edit_vessel_save_authorised_reseller($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==208){
        delete_post_meta($post_id,"wpcf-vessel-authorised-reseller");
        $vals= isset( $_POST["vessel-authorised-reseller"] ) ? $_POST["vessel-authorised-reseller"] : array();
        
        foreach($vals as $val) {
          add_post_meta($post_id, 'wpcf-vessel-authorised-reseller', $val, false);
        }      
    }
}

I've added following line to above code that should delete all post meta related post meta key "wpcf-vessel-authorised-reseller" to post ID.

delete_post_meta($post_id,"wpcf-vessel-authorised-reseller");

I hope this resolves all your issues 🙂

#581538

Minesh
Supporter

Languages: English (English )

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

I hope the solution I shared is working for you and help you to resolve your issue. 🙂 If yes - could you please mark ticket as resolved 🙂

#581540

This has worked, thank you for your help!