Support

Account

Home Forums Front-end Issues Issue with wp_new_comment and update_field (not all the fields are updated)

Helping

Issue with wp_new_comment and update_field (not all the fields are updated)

  • Hello. I have an issue with wp_new_comment and update_field.

    I am using it in this form:

    On submit I call this function:

    function submit_comment() {
    	$data = $_POST;
    	print_r($data);
    	$commentdata = array(
    		'comment_post_ID'      => $data['post_id'],
    		'comment_content'      => $data['comment_text'],
    		'comment_type'         => 'comment',
    		'user_ID'              => $data['user_id'],
    	);
    
    	$new_comment = wp_new_comment( $commentdata );
    	if($new_comment && !empty($data['rating'])) {
    	        foreach ($data['rating'] as $key => $value) {
    	            echo $key.' '.$value.' '.$new_comment.'';
    	            update_field($key,$value,'comment_'.$new_comment);
                }
        }
    	die;
    }

    as you can see everything is passing and printing from my rating fields:

    the names of the fields are the same as in the comment form backend:

    but somehow I get updated only these two fields:

    I would understand if they were updated randomly, but WHY only these two fields? What’s wrong with the others?

    When I tried to use a WP standard comment_form function with acf uncluded, everything was updated.

    May be the function has to have a sort of timeout so the update_field functions doesn’t stop?

  • I have changed the field name to field key in my “get rating fields” function:

    function get_comment_rating_fields($group_id) {
    	$rating = acf_get_fields($group_id);
    	$rating_fields = array();
    	foreach ($rating as $item) {
    		$rating_fields[] = array(
    			'field_label' => $item['label'],
    			'field_name' => $item['key'],
    			'field_min' => $item['min'],
    			'field_max' => $item['max']
    		);
    	}
    	return $rating_fields;
    }

    and everything is fine now!

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.