Skip Navigation

[Resolved] Split: Using relationship form and the add new post form, on the same page

This support ticket is created 2 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 2 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2236099

And just one more thing (apologies - I sound like Columbo )

If a Teacher can't find their school and I add a form for them to create a new School, how do I make sure they are also set as the Child of that school as well as Parent - is that another relationship form and handled automatically?

Many thanks for all the help I'm almost there I think!

#2236129

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

The way I suggested this, you don't have to automatically link the school and the teacher, at the time of new school's creation.

This is how it will work:

1. After logging in, a teacher comes to the page which has a relationship form to connect to the available schools as well as the post form to create a new school.

a). If the target school is available, the teacher can use the relationship form and the teacher and school will be linked.

b). if the target school is not available, the teacher can use the post form to create a new school. When the form will be submitted and the page will refresh, the relationship form will now have the newly added target school and the teacher can now use that form to complete the connection between the teacher and the school.

I hope this makes it more clear.

regards,
Waqar

#2236137

Many thanks for getting back to me.

One thing, is there a function that will either propulate the Relationship form with the Current user and the School they just created - or better still, on the submitting of the new School, a CRED save function to automatically link the two?

There are a few fairly similar functions I've seen on here, it's just knowing which is right for my particular need....

#2237989

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Yes, this is possible using a custom function attached to the "cred_save_data" hook, for the form to add a new post:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

You'll need the ID of the teacher post from the currently logged-in user. For this, you can register a custom shortcode. The following code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.


add_shortcode('current_teacher_id', 'current_teacher_id_func');
function current_teacher_id_func( $atts ) {
  
    $a = shortcode_atts( array(
        'type' => '',
    ), $atts );
  
    $user_post = get_posts( array( 
        'post_type'     => $a['type'], 
        'author'        => get_current_user_id(),
        'numberposts'   => 1
    ) );
  
    if(!empty($user_post)) {
        return $user_post[0]->ID;
    }
    else{
        return 0;
    }
  
}

Next, please add "current_teacher_id" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

After that, you'll be able to use this shortcode like this, to get the currently logged-in user's teacher post:


[current_teacher_id type="teacher-post-slug"]

Note: you'll replace "teacher-post-slug", with the actual slug of your teacher post type.

In the form to add a new school, you can include a hidden generic field and prefill the currently logged-in teacher's post ID, using this new shortcode.

As a result, in your custom function attached to the "cred_save_data" hook, you'll have the ID of the newly created school's post as well as the current user's teacher's post and you can create a relationship between them using the "toolset_connect_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

When the form submission will complete, the new school will be added and the current teacher's post will also be linked to that school.

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