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