Skip Navigation

[Resolved] forwarding current custom post id via hidden field to a “to-become-child” post

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

Problem:

Creating Child forms with a parent post preselected.

Solution:

You can follow our document to setup the child post form:

https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/#creating-forms-when-a-parent-post-is-preselected

Relevant Documentation:

This support ticket is created 3 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 – 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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by svenA-3 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2161545

Project: doctor's patient file management
Each patient as a registered wordpress user carrying custom roles has his patient file ('Karteikopf'). This custom post type has only one field: mirror_id, which actually is that user's wordpress ID. Each 'Karteikopf' has dependend consultation dates ('Termine').
In order to add a new 'Termin' I have a post creation form set up to enter all fields necessary for the new consultation date and a hidden field that is supposed to forward the current post ID which should be the 'Karteikopf' ID. The form is embedded in the parent 'Karteikopf' post.

The posts are layoutet as Elementor templates, the script to handle the data is hooked via cred_save_data. To check the variables, all values are added as user meta to my admin.

Result: Instead of the parent 'Karteikopf' ID the ID of another 'Termin' post is added, whis is an auto draft of the new file and contains part of the data (the new calendaric date) while the actual new post contains the rest of the data. Or briefly: the new post is split into the auto draft and the new post. And the ID passed via hidden field isn't the intended one, besides other variables come in with default values ('enddate', 1.1.1970) or empty.

The hidden field setup:
[cred_generic_field type='hidden' field='old_post_id']
{
"default":"[wpv-post-id item='$current_page']"
}
[/cred_generic_field]

The sub-function within the cred-save-data hook that handles that specific form's data:

function neu_probandentermin( $post_id, $form_data ){

$old_post_id = $_POST["old_post_id"];
$startdate = $_POST["startdate"];
$startdateUNIX = $startdate[ 'datepicker' ];
$topic = $_POST["topic"];
$topic1 = maybe_unserialize( $topic );
$topic2 = implode( ",", $topic1 );
update_user_meta( '1', 'postcheck', $old_post_id.'_'.$post_id );
update_user_meta( '1', 'topiccheck', $topic1);
update_user_meta( '1', 'datecheck', $startdateUNIX );

$postmetaarray = array(
'wpcf-eterminid' => 'keine',
'wpcf-calid' => 'keine',
'wpcf-startdate' => $startdateUNIX,
'wpcf-enddate' => $startdateUNIX + 120,
'wpcf-terminstatus' => '1',
'wpcf-topic' => $topic
);
$postarray = array(
'id' => $post_id,
'post_author' => '1',
'post_title' => get_the_title( $old_post_id ),
'post_status' => 'private',
'post_type' => 'termin',
'meta_input' => $postmetaarray,
);
wp_update_post( $postarray, true, true );
toolset_connect_posts( 'karteikopf-termin', $old_post_id, $post_id );

exit();
}

Any ideas or further questions?

kindest regards,

Sven

#2161911

Hello,

How do you setup the custom post types and post type relationships?

If the "karteikopf-termin" is one-to-many relationship, I suggest you setup the child post form:
https://toolset.com/course-lesson/selecting-parent-posts-when-using-forms-to-create-child-items/#creating-forms-when-a-parent-post-is-preselected

You can use CSS codes to hide the parent selector field, then use action hook cred_save_data to save other user information:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#2162017

Hi Luo,

I currently only have 1:n relationships (Karteikopf always on the '1' side as parent).
Now the tutorial you refer to creates a link to a page that is meant to contain the form.+

I have the form on the same page within an Elementor toggle widget as a shortcode. Can the URL parameter for the parent post ID somehow added to the forms shortcode, e. g. nested shortcode?

#2162507

I tried a different approach with the following result:

RESULT:
I still get a new 'Termin' post and beyond an AUTO DRAFT, so 2 posts with different IDs.
And the parent 'Karteikopf' gets deleted when submitting the form, which is kinda weird. As the form is set to 'keep displaying the form' without AJAX, the parent 'Karteikopf' page reload fails due to the mentioned deletion and shows the 404 page with the following URI: "hidden link"

FORM:
[credform]
<div style="display:none;">
[cred_field field='@karteikopf-termin.parent' select_text='--- not set ---' class='form-control' output='bootstrap' value="[wpv-post-id id='$current_page']"]
</div>
<div style="display:none;">
[cred_generic_field type='textfield' field='posttitle']
{
"required":0,
"default":"[wpv-post-title]"
}
[/cred_generic_field]
</div>
<div class="form-group">
<label for="%%FORM_ID%%_topic">[cred_i18n name='topic-label']Anlässe[/cred_i18n]</label>
[cred_generic_field type='checkboxes' field='topic']
{
"default":[],
"options":[{"value":"DIN EN ISO 9712","label":"DIN EN ISO 9712"},{"value":"FEV LKW","label":"FEV LKW"},{"value":"FEV PKW","label":"FEV PKW"},{"value":"G25","label":"G25"},{"value":"G41","label":"G41"},{"value":"G20","label":"G20"},{"value":"G37","label":"G37"},{"value":"G24","label":"G24"},{"value":"G42","label":"G42"},{"value":"G01","label":"G01"},{"value":"G02","label":"G02"},{"value":"G03","label":"G03"},{"value":"G04","label":"G04"},{"value":"G05","label":"G05"},{"value":"G06","label":"G06"},{"value":"G07","label":"G07"},{"value":"G08","label":"G08"},{"value":"G09","label":"G09"},{"value":"G10","label":"G10"},{"value":"G11","label":"G11"},{"value":"G12","label":"G12"},{"value":"G13","label":"G13"},{"value":"G14","label":"G14"},{"value":"G15","label":"G15"},{"value":"G16","label":"G16"},{"value":"G17","label":"G17"},{"value":"G18","label":"G18"},{"value":"G19","label":"G19"},{"value":"G21","label":"G21"},{"value":"G22","label":"G22"},{"value":"G23","label":"G23"},{"value":"G26","label":"G26"},{"value":"G27","label":"G27"},{"value":"G28","label":"G28"},{"value":"G29","label":"G29"},{"value":"G30","label":"G30"},{"value":"G31","label":"G31"},{"value":"G32","label":"G32"},{"value":"G33","label":"G33"},{"value":"G34","label":"G34"},{"value":"G35","label":"G35"},{"value":"G36","label":"G36"},{"value":"G38","label":"G38"},{"value":"G39","label":"G39"},{"value":"G40","label":"G40"},{"value":"G43","label":"G43"},{"value":"G44","label":"G44"},{"value":"G45","label":"G45"},{"value":"G46","label":"G46"},{"value":"ArbZG","label":"ArbZG"}]
}
[/cred_generic_field]
</div>
<div class="form-group">
<label for="%%FORM_ID%%_startdate">[cred_i18n name='startdate-label']startdate[/cred_i18n]</label>
[cred_field field='startdate' force_type='field' class='form-control' output='bootstrap']
</div>
[cred_show_group if="( $(post_title) eq '' )" mode="fade-slide"]
<div class="form-group">
<label for="%%FORM_ID%%_eterminid">[cred_i18n name='eterminid-label']eterminID[/cred_i18n]</label>
[cred_field field='eterminid' force_type='field' class='form-control' output='bootstrap' value='keine']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_calid">[cred_i18n name='calid-label']calID[/cred_i18n]</label>
[cred_field field='calid' force_type='field' class='form-control' output='bootstrap' value='keine']
</div>
<div class="form-group">
<label for="%%FORM_ID%%_terminstatus">[cred_i18n name='terminstatus-label']Terminstatus[/cred_i18n]</label>
[cred_field field='terminstatus' force_type='field' class='form-control' output='bootstrap' value='Termin offen / verschoben']
</div>[/cred_show_group]
[cred_field field='form_submit' output='bootstrap' value='Absenden' class='btn btn-primary btn-lg']
[/credform]

CRED_SAVE_DATE sub-function:
function neu_probandentermin( $post_id, $form_data ){

$postinput = file_get_contents('hidden link');
parse_str($postinput, $postcheck);
$postcheck = apply_filters( 'wp_parse_str', $postcheck );
$startdate = $postcheck["wpcf-startdate"];
$startdateUNIX = $startdate[ 'datepicker' ];
$enddate = $startdateUNIX + 120;
$topic = $postcheck["topic"];
$topic1 = maybe_unserialize( $topic );
$topic2 = implode( ",", $topic1 );
$title = $postcheck["posttitle"];
update_user_meta( '1', 'datecheck', $startdateUNIX );
update_user_meta( '1', 'titlecheck', $title );
update_user_meta( '1', 'postcheck', $postcheck );

$postmetaarray = array(
'wpcf-eterminid' => 'keine',
'wpcf-calid' => 'keine',
'wpcf-startdate' => $startdateUNIX,
'wpcf-enddate' => $enddate,
'wpcf-terminstatus' => '1',
'wpcf-topic' => $topic2
);
$postarray = array(
'id' => $post_id,
'post_author' => '1',
'post_title' => $title,
'post_status' => 'private',
'post_type' => 'termin',
'meta_input' => $postmetaarray,
);
wp_update_post( $postarray, true, true );

return;
}

#2162511

I suggest you try these:
1) remove the custom PHP codes you mentioned above, test the post form in frontend, check if it works fine.

2) If it does not work, get the PHP debug logs from your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/#php-debugging
And check the compatibility issue in your website: deactivate all other plugins, and switch to WordPress default theme 2021

3) If it does work, then debug your custom PHP codes line by line, especially the last few lines:

$postarray = array(
'id' => $post_id,
'post_author' => '1',
'post_title' => $title,
'post_status' => 'private',
'post_type' => 'termin',
'meta_input' => $postmetaarray,
);
wp_update_post( $postarray, true, true );
#2163181

The major part is working now with the form including CSS-hidden title and content fields which has the auto_draft vanish:

[credform]
<div style="display:none;">
[cred_field field='@karteikopf-termin.parent' select_text='--- not set ---' class='form-control' output='bootstrap' value="[wpv-post-id id='$current_page']"]
[cred_field field='post_title' class='form-control' output='bootstrap' value='[wpv-post-title]']
[cred_field field='post_content' output='bootstrap' value='[wpv-post-title]']
</div>
.......

sub function now looks like this after deleting the post creation part and only updating meta data:

function neu_probandentermin( $post_id, $form_data ){

$postinput = file_get_contents('hidden link');
parse_str($postinput, $postcheck);
$postcheck = apply_filters( 'wp_parse_str', $postcheck );
$startdate = $postcheck["wpcf-startdate"];
$startdateUNIX = $startdate[ 'datepicker' ];
$enddate = $startdateUNIX + 3600;
$topic = $postcheck["topic"];
$topic1 = maybe_unserialize( $topic );
$topic2 = implode( ",", $topic1 );

update_post_meta( $post_id, 'wpcf-startdate', $startdateUNIX );
update_post_meta( $post_id, 'wpcf-enddate', $enddate );
update_post_meta( $post_id, 'wpcf-topic', $topic2 );
update_post_meta( $post_id, 'calid', 'keine' );
update_post_meta( $post_id, 'eterminid', 'keine' );
update_post_meta( $post_id, 'wpcf-status', 1 );

return;
}

Result: I can create a new, 'Karteikopf' dependend 'Termin' post that contains all data passed by the form.
One issue remains and that's the time of the Unix time stamp:
While the calendaric date matches the form input, time is always 0:00 no matter what I enter in the form. The 'enddate' calculation works but on the 0:00 value of the 'startdate'.
What am I missing here?

regards,

Sven

#2163183

Never mind, solved it that way:

$startdateUNIX = $startdate[ 'datepicker' ] + 3600 * $startdate[ 'hour' ] + 60 * $startdate[ 'minute' ];

However, I thought the date field delivered a full datetime value ...

#2163263

The Toolset custom date fields store value in timestamp format, is this problem resolved? please let me know if you still need assistance for it

#2163739

My issue is resolved now. Thank you!