Skip Navigation

[Resolved] cred_save_data and relationship

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

Problem:

The issue here is that the user is pulling the relationship data in their CRED hook but its not pulling the current set relationship but the previous one.

Solution:

If this is happening to you I would suggest that you change your CRED hook to one that fires later.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

This support ticket is created 5 years, 4 months 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 18 replies, has 2 voices.

Last updated by Pat 5 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1270775

Pat

Hello,

I'm using a relationship in a Cred form and need to use a cred_save_data to modify some post parameters (including those related to the relationship).

I have tried a first submission and it seems that the cred_save_data is not able to retrieve the relationship info. Then I launch the form another time and the right relationship was recorded.

It seems that when you are defining the relationship in the form for the first time, the cred_save_data is not able to get the info?
Can you check and tell me if this is the case and how to use the relationship inside a Cred and get the right value the first time the form is submitted.

Regards
Pat

#1271069

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Thank you for getting in touch.

What part of the relationship info you are trying to get. Could you provide a little more details on what you are trying to do so that I can point you in the right direction?

Thanks,
Shane

#1271249

Pat

Hi Shane,

I have defined a relationship between product and salle (salle can have several products).
I have a post form (modify) for stage in which I placed the relationship field.
Then, I have a snippet (cred_save_data) to built the created post title thanks to some info, including salle.
Here is the code :

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

if (($form_data['id']==328) OR ($form_data['id']==1027))
{ 

$date_debut = $_POST['wpcf-date-debut-stage']['datetime'];

$titre ="";
$salle_id = toolset_get_related_post( $post_id, $relationship='salle-product' );
$salle = get_the_title($salle_id);  
$user_id = get_current_user_id();
  
$titre = 'Stage du '.$date_debut." à ".$salle;
  	  error_log( print_r(  "Titre : ".$titre."Date debut ".$date_debut , true ) );
$slug = strtolower($titre);
$slugstring = str_replace(' ', '-', $slug);
  
$args = array(
  'ID' => $post_id, 
  'post_title' => $titre, 
  'post_name' => $slugstring,
  'post_author' => $user_id
);

wp_update_post($args);
}
}

During the first submission, instead of the salle value, I'm getting the post title of the current modify post. Then, if I launch a seconde submission, then the right relationship info is recorded.

Again, it seems like the relationship info was not available during the first submission !

Any idea?
Regards
Pat

#1271739

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

I suspect the issue is with getting the parent data,

However when you say "first submission". does that mean if you resubmit the form it works fine ?

Please let me know.

Thanks,
Shane

#1271749

Pat

Hi Shane,
You're right, if I re-submit the form without changing anything, that's fine.
Regards
Pat

#1272769

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

So your hook works.
Could you test it like this. I'm adding a Die function to see if it fires all the time or only on the second attempt.


add_action('cred_save_data','func_stage_title',10,2);
function func_stage_title($post_id,$form_data) {
 
if (($form_data['id']==328) OR ($form_data['id']==1027))
{ 
 die('this hook works');
$date_debut = $_POST['wpcf-date-debut-stage']['datetime'];
 
$titre ="";
$salle_id = toolset_get_related_post( $post_id, $relationship='salle-product' );
$salle = get_the_title($salle_id);  
$user_id = get_current_user_id();
   
$titre = 'Stage du '.$date_debut." à ".$salle;
      error_log( print_r(  "Titre : ".$titre."Date debut ".$date_debut , true ) );
$slug = strtolower($titre);
$slugstring = str_replace(' ', '-', $slug);
   
$args = array(
  'ID' => $post_id, 
  'post_title' => $titre, 
  'post_name' => $slugstring,
  'post_author' => $user_id
);
 
wp_update_post($args);
}
}

Please replace your hook with this and let me know if the die('this hook works'); on the first attempt.

If it doesn't then try your function without the 2 form ID and just test with only one ID.

Thanks,
Shane

#1273011

Pat

Hi Shane,

I tried with your code and the result is the following :
- First submission : blank page
- Reload of the same page => this hook works
- But the title is not inline with the value of the relationship field

Then I used only the form 1027 (and placed directly cred_save_data_1027 and then :
- First submission : => this hook works
- But the title is still not in line with the relationship field !

So, the situation is always the following :
- when submitting the form, the relationship field is well saved
- the title is not updated with the last value of the relationship field

That sounds strange as I have another ticket that have been treated by Christian : https://toolset.com/forums/topic/limit-the-relationship-list-in-a-form/
In this ticket, I can in the same cred_save_data, create the relationship and use it to built the title !

Regards
Pat

#1273653

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Would you mind allowing me to have access to the site to test this for you ?

I see no reason why the hook shouldn't work on your first try.

Thanks,
Shane

#1273837

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

I tried the credentials you had sent but i'm not able to log in with them.

Could you check on them and let me know if they work for you.

Thanks,
Shane

#1273871

Pat

Hi Shane,

I have re-generated the password as indicated in the private message. Please try again and tell me if something is wrong.
Regards
Pat

#1275837

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

Thanks, I was able to get onto the site now, however I don't want to modify your existing data with the edit form.

Could you let me know where I can find the form with ID 328 on the frontend?

Thanks
Shane

#1275851

Pat

Hi Shane,

You have it in the backoffice/gestion-des-stages/
It is integrated inside a modal window.
Just click on "Modif" in the right of the first line (Stage du 2019-06-19 à Saint Quentin Fallavier). As it is a test post, you cna use it to make some tests.
Regards
Pat

#1275867

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Pat,

You code is working fine from what I can see.

Even on my first submit it displays the correct data.

I used this to test it.

  die('ID: '.$post_id.' post_title: '.$titre.'post_name: '.$slugstring.'post_author: '.$user_id); 
wp_update_post($args);

This way I can know what exactly is being passed before the post is updated.

Thanks,
Shane

#1275891

Pat

Hi Shane,

Not sure to have been sufficiently clear. First, I made a mistake : this is not inside a modal window, but redirect to the product page for modification (produit/stage-du-2019-06-19-a-vaulx-en-velin-salle-1/?content-template-id=1030)

I just tried to modify a stage (page backoffice/gestion-des-stages/ and click on Modif in the right).
The initial title was : Stage du 2019-06-19 à Vaulx en Velin – Salle 1

Then, I change "Lieu du stage" from Vaulx en Velin – Salle 1 to Saint Quentin Fallavier and click on MODIF.
Nothing changed, I'm getting a page with : ID: 1040 post_title: Stage du 2019-06-19 à Vaulx en Velin – Salle 1post_name: stage-du-2019-06-19-à-vaulx-en-velin-–-salle-1post_author: 1
And nothing is changed in the post (relationship salle-product not updated and post title not updated neither !).

Regards
Pat

#1276001

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2019-06-24 at 1.20.45 PM.png
Screenshot 2019-06-24 at 1.20.37 PM.png

Hi Pat,

It works for me take a look at the screenshots.

I renamed the post to Test and then on the frontend modified it and just hit save and the title changed.

I'm assuming this is the result that you want.

Thanks,
Shane