Skip Navigation

[Resolved] Creating parent and child post with parent form

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 3 replies, has 2 voices.

Last updated by Minesh 10 months ago.

Assisted by: Minesh.

Author
Posts
#2684112

Dear Toolset team,

I have on my website two CPT ‘aktivitat’ (this is Germany for activity) and ‘bewertung’ (this is German for rating). What I need is that the user can create in the same CRED form a new activity and also the rating for it. The relationship slug is ‘aktivitat-bewertung’.

For archieving this I used the following code which created both posts but in the ‘bewertung’ the parent is missing. Can you please check if my code is wrong?

function create_bewertung_post($post_id, $form_data) {

if ($form_data['id'] == 217) {

$bewertung_value = $_POST['wpcf-in-aktivitat-bewertung-a'];

$post_title = 'Bewertung für Aktivität: ' . get_the_title($post_id);

$new_post_args = array(
'post_title' => $post_title,
'post_status' => 'publish', // Veröffentlichten Beitrag erstellen
'post_type' => 'bewertung', // Post-Typ 'bewertung'
);

$new_post_id = wp_insert_post($new_post_args);

if (!is_wp_error($new_post_id)) {

update_post_meta($new_post_id, 'wpcf-aktivitat-bewertung-a', $bewertung_value);

update_post_meta($new_post_id, 'aktivitat-bewertung_parent', $post_id);
}
}
}
add_action('cred_submit_complete', 'create_bewertung_post', 10, 2);

#2684253

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

To connect the related post can use the Toolset post-relationship API hook:

It seems that you have "aktivitat" setup as parent post type and "bewertung" is setup as child post type.

The form with ID "217" seems to setup to create/update "aktivitat" post type post where you are using the hook "cred_submit_complete" to create a child post "bewertung" and you want to connect the child post with parent post.

What if you try to use the following code - You may try to add the following code to "Custom Code" section:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

function create_bewertung_post($post_id, $form_data) {

if ($form_data['id'] == 217) {

$bewertung_value = $_POST['wpcf-in-aktivitat-bewertung-a'];

$post_title = 'Bewertung für Aktivität: ' . get_the_title($post_id);

$new_post_args = array(
'post_title' => $post_title,
'post_status' => 'publish', // Veröffentlichten Beitrag erstellen
'post_type' => 'bewertung', // Post-Typ 'bewertung'
);

$new_post_id = wp_insert_post($new_post_args);

if (!is_wp_error($new_post_id)) {

update_post_meta($new_post_id, 'wpcf-in-aktivitat-bewertung-a', $bewertung_value);

///connecting parent with child
toolset_connect_posts( 'aktivitat-bewertung', $post_id, $new_post_id );

}
}
}
add_action('cred_submit_complete', 'create_bewertung_post', 10, 2);

More info:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

#2684467

Hi Minesh,

this is working, thanks. I also modified the code that now also the field ‘wpcf-bewertung-a' from CPT ‘bewertung’ is updated, see the modified code here:

function create_bewertung_post($post_id, $form_data) {

if ($form_data['id'] == 186) {

$bewertung_value = $_POST['wpcf-in-beitrag-bewertung-a'];

$post_title = 'Bewertung für Aktivität: ' . get_the_title($post_id);

$new_post_args = array(
'post_title' => $post_title,
'post_status' => 'publish', // Veröffentlichten Beitrag erstellen
'post_type' => 'bewertung', // Post-Typ 'bewertung'
);

$new_post_id = wp_insert_post($new_post_args);

if (!is_wp_error($new_post_id)) {

update_post_meta($new_post_id, 'wpcf-in-beitrag-bewertung-a', $bewertung_value);

///connecting parent with child
toolset_connect_posts( 'post-bewertung', $post_id, $new_post_id );

// Kopieren des Feldwerts
$bewertung_a = get_post_meta($post_id, 'wpcf-in-beitrag-bewertung-a', true);
update_post_meta($new_post_id, 'wpcf-bewertung-a', $bewertung_a);

}
}
}
add_action('cred_submit_complete', 'create_bewertung_post', 10, 2);

What unfortunately not working is, that when a user creates a ‘bewertung’ (in English a rating) with the CRED form ‘186’ which is for activity posts, the fields 'wpcf-bewertung-aktivitat-a-ds' and 'wpcf-bewertung-aktivitat-a-ttl' of the parent post are not upated. When I submit a ‘bewertung’ with the CRED form ‘320’ which is for directly create ‘bewertung’ then the mentioned fields get updated thanks to the code

function update_posta_review($post_id, $form_data) {
// if specific form, change ID to the CRED "bewertung" ID
if ($form_data['id'] == 320) {
// Get ID of aktivitaten Being Reviewed
$parent_post = $_POST['@post-bewertung_parent'];

$aktivitaten = toolset_get_related_posts($parent_post, 'post-bewertung', 'parent', 999, 0, array(), 'post_object', 'child');

$sum = 0;
$num = 0;
foreach ($aktivitaten as $aktivitat) {
$bewertungen = get_post_meta($aktivitat->ID, 'wpcf-bewertung-a', true);
if ($bewertungen) {
$sum += $bewertungen;
$num++;
}
}
$average = ($num > 0) ? $sum / $num : 0;

// Rundung auf die nächste Zehntelstelle (0,1)
$res = round($average, 1);

update_post_meta($parent_post, 'wpcf-bewertung-aktivitat-a-ds', $res);
update_post_meta($parent_post, 'wpcf-bewertung-aktivitat-a-ttl', $num);
}
}
add_action('cred_submit_complete', 'update_posta_review', 10, 2);

Is there something I can do, that the fields 'wpcf-bewertung-aktivitat-a-ds' and 'wpcf-bewertung-aktivitat-a-ttl' of the parent post are always updated regardless which CRED form I used.

Best whishes
Andreas

New threads created by Minesh and linked to this one are listed below:

https://toolset.com/forums/topic/split-creating-parent-and-child-post-with-parent-form-rating-not-working/

#2684612

Minesh
Supporter

Languages: English (English )

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

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery for the original issue reported.

I'll split the ticket with your new question and you are welcome to mark resolve this ticket. We will continue with the split ticket here:
- https://toolset.com/forums/topic/split-creating-parent-and-child-post-with-parent-form-rating-not-working/

#2684888

Yes, sure my initial issue was solved well

Thank you very much Minesh!