Passer la navigation

[Résolu] Split: Creating parent and child post with parent form – rating not working

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
Update fields with "cred_submit_complete" hook

Solution:

You can find the proposed solution in this case with the following reply:
- https://toolset.com/forums/topic/split-creating-parent-and-child-post-with-parent-form-rating-not-working/#post-2684934

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete

This support ticket is created Il y a 2 years, 3 months. 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
- 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 -

Fuseau horaire du supporter : Asia/Kolkata (GMT+05:30)

Ce sujet contient 1 reply, a 2 voix.

Dernière mise à jour par Minesh Il y a 2 years, 3 months.

Assisté par: Minesh.

Auteur
Publications
#2684610

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

#2684934

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you add the code you shared with your another hook and add that code to your desired hook and just change the parent ID and child ID variables and check if that helps.

#2685197

Hi Minesh,

ok, I could fix it now. Thanks for your support

Best wishes
Andreas