Skip Navigation

[Resolved] Creating relationship between form and post

This support ticket is created 4 years, 3 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 8 replies, has 2 voices.

Last updated by Chris 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1840591

Based on my previous support request, I am trying to do the same thing with another form & post type.
I have duplicated the custom code as below but it isn't working. Any idea where I have gone wrong?
I have updated the function name, form ID and the toolset_connect_posts name
thanks

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

if ($form_data['id']==15800){
$current_user_id = get_current_user_id();
$args = array(
'author' => $current_user_id,
'post_type' => 'profile',
'fields' => "ids");

$found_profile = get_posts($args);
if(count($found_profile) > 0){
toolset_connect_posts( 'profile-country-report',$found_profile[0], $post_id );
}

}

}

#1841481

Shane
Supporter

Languages: English (English )

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

Hi Chris,

Thank you for getting in touch.

I suspect the issue is right here

$found_profile = get_posts($args);
if(count($found_profile) > 0){
toolset_connect_posts( 'profile-country-report',$found_profile[0], $post_id );

You will perhaps need to exit the code while it's running to see what happens.
Can you add this line below and then run the form to see what gets dumped out on the page.

var_dump($found_profile);

Also what is this argument for?
'fields' => "ids"

Thanks,
Shane

#1841671

Hi Shane
I'm not sure what the 'fields'=>"ids" argument is for, I have just reused the same snippet supplied in my previous support request to achieve the same thing (with different posts).

If I add this to the code:
var_dump($found_profile[0]);
var_dump($post_id);

I get the result of int(1512) int(15835) which is the Profile ID and the Post ID so I'm guessing something is wrong with the toolset_connect_posts command. I have created the relationship with the slug of profile-country-report, the only difference of it compared to the one I had for the previous support request, is that this one is a one-to-one relationship not a one-to-many. Should that make any difference?

thanks

#1842185

Shane
Supporter

Languages: English (English )

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

Hi Chris,

Assuming that the ids that you are using are correct then we can safely say the correct values are being passed to the function.

Would you mind allowing me to have admin access to the site so that I can check on this for you in further details ?

I've enabled the private fields for your next response. Also please let me know the page that you are testing this out on.

Also let me know the original support ticket where you got this code from so that I can have a little background reference.

Thanks,
Shane

#1843557

Shane
Supporter

Languages: English (English )

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

Hi Chris,

The reason why it wasn't working was because of this error message.

 "The element Britain has already the maximum allowed amount of associations (1) as in the relationship Profiles Country Reports." 

It should be working now. What I had to do was to clear up some of the trash posts that were connected to the britain user.

So in order to allow britain to connect to a country report relationship you will need to delete my "Final test" post and then remove it from the trash as well.

Thanks,
Shane

#1846777

Thanks, I can't believe it was such an obvious thing, once you know.
Can I ask though where did you get that error message from, I never saw it anywhere.

thanks
Chris

#1847555

Shane
Supporter

Languages: English (English )

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

Hi Chris,

The toolset_connect_posts() function will actually return an error if it fails.

However in order for me to see the error I had to dump it out onto the page using

var_dump(toolset_connect_posts( 'profile-country-report',$found_profile[0], $post_id );)

Once I did this I was able to see what exactly was happening inside the function.

Thanks,
Shane

#1847859

Awesome, thanks

#1847861

My issue is resolved now. Thank you!