Skip Navigation

[Résolu] Updating Expiry Date on One Post With Expiry Date of A Related Post

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Updating Expiry Date on One Post With Expiry Date of A Related Post

Solution:
You can use the Toolset post relationship API function "toolset_get_related_post" to get the child ID.

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/updating-expiry-date-on-one-post-with-expiry-date-of-a-related-post/#post-1106563

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

This support ticket is created Il y a 6 années et 2 mois. 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
- 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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 6 années et 2 mois.

Assisté par: Minesh.

Auteur
Publications
#1105485

I have two post types in a one to one relationship. Both have expiration enabled. When Post A (which already has an expiry date) is amended (at a point in time when Post B has already been created), I want its expiry date to be changed to match the expiry date of Post B.

I've tried it like this:-

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

    // This is the form that updates Post A
    if ($form_data['id']==519)    {
	
	$related_post_id = toolset_get_related_post($post_id, 'a-b' );
	$a_expiry = get_post_meta($related_post_id, '_cred_post_expiration_time', true);
	
	// update expiry date
       update_post_meta($post_id, '_cred_post_expiration_time', $a_expiry);
	}
}

but this removes the expiry for the post.

I've double checked that the relationship exists but can't work out why this isn't functioning. Any ideas please? Thanks

#1106377

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as I understand the form you currently editing is the parent post form. So, you want to update the _cred_post_expiration_time to the related post - correct? If yes:

Could you please try to use following code:

add_action('cred_save_data', 'save_data_form_519',10,2);
function save_data_form_519($post_id, $form_data) {
 
    // This is the form that updates Post A
    if ($form_data['id']==519)    {
     
    $related_post_id= toolset_get_related_post($post_id, 'a-b','child');
    $a_expiry = get_post_meta($related_post_id, '_cred_post_expiration_time', true);
     
    // update expiry date
       update_post_meta($related_post_id, '_cred_post_expiration_time', $a_expiry);
    }
}
#1106563

Hi Minesh

Thanks for looking into this for me. I had to make a slight adjustment to your code and it works so thank you for your guidance here. Here's the final code:-

add_action('cred_save_data', 'save_data_form_519',10,2);
function save_data_form_519($post_id, $form_data) {
  
    // This is the form that updates Post A
    if ($form_data['id']==519)    {
      
    $related_post_id= toolset_get_related_post($post_id, 'a-b','child');
    $a_expiry = get_post_meta($related_post_id, '_cred_post_expiration_time', true);
      
    // update expiry date
       update_post_meta($post_id, '_cred_post_expiration_time', $a_expiry);
    }
}

I'm curious as to why I needed to add 'child' when retrieving the related post. I have other hooks containing this line (in one-to-many relationships). Should I be adding 'child' or 'parent' to these other hooks to avoid incorrect output even though this isn't indicated anywhere? I'd really like to understand this better please. Thanks.

#1106566

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - as you can see with the Doc for toolset_get_related_post API function:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

The defult is parent , so whenever you want to have parent ID, if you will not pass the argument it's OK but when you want to have related child post ID, you need to pass child as argument as default is parent.

#1106571

ah OK - thank you for clarifying!