Skip Navigation

[Resolved] Forms to create a custom post and a related custom post

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)

This topic contains 4 replies, has 1 voice.

Last updated by davidL-7 1 week, 6 days ago.

Assisted by: Minesh.

Author
Posts
#2792565

Tell us what you are trying to do?

Hi, I'm struggling with the implementation of a form to create a custom post that is related to a custom post I've just created. I've reviewed the documentation and struggled with it, and finally decided to reach out for support.

Is there any documentation that you are following?

Yes, I've been trying to follow along with https://toolset.com/lesson-placement/lesson-placements-1824673-1727277/#when-you-need-forms-from-connecting-between-post and https://toolset.com/course-lesson/front-end-relationship-forms-for-connecting-posts, but I'm not understanding how to create and implement the relationship form.

Is there a similar example that we can see?

Yes. I'm rebuilding an old app that was originally built in CMFL. You can access a clone of the app (feel free to add data) here:

access details removed.

Here's the basic schema:

An Aid Request can have many Aid Items. You can create a New Request (feel free to click the New Request link and add one). Then click Add Items, and you can then add multiple Aid Items to the Aid Request.

What is the link to your site?

My work-in-progress is at :
hidden link (Note, this is a development site and you can feel free to add what's needed if desired.)
u: toolsetsupport
p: wy6)oWsnpU6vBBe##75$)STY

Here's what I've got:
Custom Post Type: Aid Request
Custom Post Type: Aid Items
Relationship: (One) Aid Request to (Many) Aid Items
Form to add an Aid Item (working successfully, using a few hooks under Settings to add some additional field values on submit)

From the home page (the Admin Index), you can click New Request (hidden link) to create a new Aid Request post.

Here's where I'm getting stuck. The next thing I need the user to be able to do is to add Aid Items that will be related to the post just created. I assume I need a Relationship Form, but after trying to follow the documentation and studying some other support posts, I can't figure out how to put it all together.

I should note that on the old app, on adds an Aid Request (data gets inserted), then adds Aid Items in a second step. I'm okay emulating this two step process (and I created a rough Add Aid Item form which you can hit at hidden link), but I'd also be okay (in fact it might be more elegant) to add the Aid Items as a repeating field (since there can be many Aid Items) on the same page where the Aid Request is created (hidden link). Either way is fine.

It's also worth noting that most of the Aid Requests and Aid Items posts you'll see in the system were imported from the legacy system via WP All Import.

Of course Aid Requests and Aid Items will need to be edited, but I figure I should get past this hurdle first. Any help would be appreciated!

Thanks,
David

#2792654

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know that you need help connecting post in one-to-many post relaitioinhsip or many-to-many post relationship?

#2792689
Screenshot 2025-01-17 at 11.24.26 AM.png
Screenshot 2025-01-17 at 11.21.38 AM.png

Hi Minesh,

It's a one-to-many relationship. The relationship is already set up successfully (sees screenshot). I had existing data for both post types (Aid Requests and Aid Items), so I imported them with WP All Import and was able to successfully establish the relationship (see second screenshot of a typical edit screen for an Aid Request post, showing a related Aid Item).

As noted above, I've successfully created a form to add an Aid Request post (hidden link; that's the one in the one-to-many relationship), but am not sure how to add Aid Items (the many in the relationship) in such a way that they get related to their Aid Request.

I assume that the Relationship Form comes into play somehow, but I can't figure out how to use it. I've also set up a second form for adding Aid Items (hidden link), thinking that the Relationship Form might connect those two forms somehow, but am lost as to how to make it all work.

Thanks!
David

#2792719
Screenshot 2025-01-17 at 11.53.14 AM.png

Hi Minesh,

I've been working on this while waiting for your reply and am closer, though I'm still having an issue which I suspect will be easy for you to solve (but I'm stuck again). Here's what I did:

1) As noted above, I set up second for for adding an Aid Item custom post (which will need to relate to an Aid Request). I generated the form with the wizard, and when I was studying it, noted that the wizard had added a field for establishing the relationship (see screenshot).

2) So my strategy is to change that field for a hidden field. If I can redirect the user on submit of the add Aid Request form to the Add Item form and pass the value I need in the query string, then I can put that value into the hidden field, and thus establish the relationship.

3) The value of that hidden field is actually the post_title of the post that gets created when an Aid Request post is created. I've been able to successfully get a redirect, but I'm not succeeding in getting the post_title. Here's the code of my function:

<pre>
function tssupp_custom_redirect( $url, $post_id, $form_data ){

if ( $form_data['id'] == '21798' ) { // Aid Requests v3 (ID: 21798)
// $requestid = $form_data['post_title']; This didn't work, got no value.
$requestid = $_POST['wpcf-post_title']; // This didn't work either.

$url .= '?requestid=' . $requestid;
}

return $url;
}
add_filter( 'cred_success_redirect', 'tssupp_custom_redirect', 10, 3 );
</pre>

Can you tell me how to get the post title of the post just created from submitting form 21798?

Thanks!
David

#2792727
Screenshot 2025-01-17 at 1.45.02 PM.png

Hi again Minesh,

I answered my own last question with the help of https://toolset.com/forums/topic/save-update-the-featured-image-title-with-the-post-title/ and have a workable solution to the original question. Here's what I did:

1) Set the first add Aid Request form (the "one" in the one-to-many relationship) to redirect to the Add Aid Item form (Aid Items being the "many").

2) Added custom code to include the post title, which is the value the relationship is using to match the Item to the Request like so:

function tssupp_custom_redirect( $url, $post_id, $form_data ){
if ( $form_data['id'] == '21798' ) { // Aid Requests v3 (ID: 21798)
$requestid = esc_html( get_the_title($post_id) );
$url .= '?requestid=' . $requestid;
}
return $url;
}
add_filter( 'cred_success_redirect', 'tssupp_custom_redirect', 10, 3 );

3) Replaced the Relationship select field generated by the wizard when I created the Add Aid Item form with a custom generic field:

[cred_generic_field type='hidden' field='@aid-request-aid-item.parent']
{
"default":"[wpv-search-term param="requestid"]"
}
[/cred_generic_field]

Unfortunately it's not a success, in that the relationship was not created after submitting the Aid Item form. I had viewed the source code to confirm that my requestid made it into my generic hidden field, which it did:

<input type="hidden" id="cred_form_21811_1_1_aid-request-aid-itemparent" name="@aid-request-aid-item.parent" value="1737149469483" class="wpt-form-hidden form-hidden" data-wpt-id="cred_form_21811_1_1_cred_form_21811_1_1_aid-request-aid-itemparent" data-wpt-name="@aid-request-aid-item.parent">

However, submitting this is apparently not the same thing as selecting it from the original pulldown menu generated from the wizard, which looked like this:

<div class="js-wpt-field-items js-wpt-repetitive wpt-repetitive" data-initial-conditional="" data-item_name="select-@aid-request-aid-item.parent">
<select id="cred_form_21811_1_1_aid-request-aid-itemparent" preset_value="" urlparam="" make_readonly="" max_width="" max_height="" class="form-control wpt-form-select form-select select toolset_select2_prefix_58379 toolset_select2_converted toolset_select2-hidden-accessible" output="bootstrap" select_text="--- not set ---" data-orderby="title" data-order="ASC" data-author="" data-wpt-type="select" name="@aid-request-aid-item.parent" tabindex="-1" aria-hidden="true">
<option value="" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_21811_1_1_cred_form_21811_1_1_aid-request-aid-itemparent" data-wpt-name="@aid-request-aid-item.parent" selected="selected">--- not set ---</option>

(...followed by an ajax search of Aid Request post titles, see screenshot).

I'm not sure why my hidden form field didn't work (maybe the name attribute value is wrong?). And there may be a better way of establishing the relationship (maybe using Relationship Forms?). The method I'm working on seems to be a valid workaround, but if you can provide better instructions, I would really appreciate it!

Thanks,
David

#2793123

Minesh
Supporter

Languages: English (English )

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

Can you please send me problem URL where you added the form as well as what form you are using and admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2793258

Minesh
Supporter

Languages: English (English )

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

May I know why you are passing the post title with the code that you use?

function tssupp_custom_redirect( $url, $post_id, $form_data ){
 
    if ( $form_data['id'] == '21798' ) { // Aid Requests v3  (ID: 21798)
        $requestid = esc_html( get_the_title($post_id) );
        $url .= '?requestid=' . $requestid;
    } 
 
    return $url;
}
add_filter( 'cred_success_redirect', 'tssupp_custom_redirect', 10, 3 );

Is there any specific reason? if no, it should bbe the post ID. so the URL should be generated using the post ID of request ID.

#2793367

Hi Minesh,

Yes, good question.

The reason I'm passing the post title, is because the value of the post title for the Aid Requests posts (the one in the one-to-many) are the value that will be used in establishing the relationship. (In other words, in the Relationship that has been set up, when the system goes looking for which Aid Request post that the Aid Item being added should be related to, that post_title value is the unique identifier.

But yes, if you could show me how to do it using the postID, that would make more sense. To re-cap, here's the flow I'm going for:

1) User adds an Aid Request at hidden link
2) On submission/creation of Aid Request post (the one), user is taken to Add Aid Item form (hidden link), where they will be able to add one or more Aid Item posts (the many) that are related to the Aid Request (the one) that was created at the start of the process.

Thanks for the help,
David

#2793368

Hi Minesh,

Wow! After I posted my response, I got back into it and saw that you had customized the function (and restored the hidden field to the Add Item form). I cloned the Add Item form and the function, sent the post_id instead of the requestid as you had indicated, and had success!

Thank you so much, I would have never figured that out on my own (especially the auto connect to parent function you wrote)! I'm including that here for the benefit of whomever else may read this (very long) thread:

function tssupp_custom_redirect( $url, $post_id, $form_data ){

if ( $form_data['id'] == '21798' ) { // Aid Requests v3 (ID: 21798)
//$requestid = esc_html( get_the_title($post_id) );
$url .= '?postid=' . $post_id;
}

return $url;
}
add_filter( 'cred_success_redirect', 'tssupp_custom_redirect', 10, 3 );

add_action('cred_save_data', 'tssupp_auto_connect_to_parent_',10,2);
function tssupp_auto_connect_to_parent_($post_id, $form_data) {
$forms = array( 21865 );
if ( in_array( $form_data['id'], $forms ) ){

$parent_id = $_POST["aid_request_parent_id"];
toolset_connect_posts( 'aid-request-aid-item', $parent_id, $post_id);
}
}

Thanks again! I still need to figure out how to loop the user through the option of adding multiple Aid Items, but if I have problems, I'll start a new support ticket, as this one is quite long. I really appreciate your help!

David