Home Forums ACF PRO Save/update field of custom field with function Reply To: Save/update field of custom field with function

  • I was already afraid of that, it all sounds rather complicated and maybe it is. But this is the way I set it up and basically it works now only I think the code (as you say) is not good.

    I have added some comment to the code, maybe it is more clear.

    I have also added an image that might be a better idea of how it is set up and what the intention is.

    The ultimate goal is to make a filter with the average score of each hosting package. So that someone can filter on the average score of each package.


    function when_a_review_gets_submitted ($review, $post_id) {
    // Set up the objects needed
    $hosting_provider_query = new WP_Query();
    global $post;
    $hosting_pagina_titel = $post->post_title;  
    $search_all_pages = $hosting_provider_query->query(array(
        'post_type' => 'page',
        'post_status' => 'publish',
    	//Only get pages where custom field 'name_hosting_provider' equals Page title
        'meta_query' => array(
    		'relation' => 'AND',
                'key' => 'naam_hosting_provivder',
                'value' => $hosting_pagina_titel,
    			'compare'	=> '='
    // Loop through all pages and find Page's children
    $loop_through_all_child_pages = get_page_children( get_the_ID(), $search_all_pages );
    // Loop through everything we got back
        foreach($loop_through_all_child_pages as $child_page){
    		// get the ID of each childpage
    		$get_child_page_ID = $child_page->ID;
    		// get the ID of each parent page of the child pages
    		$get_parent_page_ID = get_queried_object_id();
    		// Get the average score from post_meta of the parent page
    		$get_average_score = get_post_meta( $get_parent_page_ID, 'rwp_user_score', true );
    		// update each custom field of the childs with the average score data from the post meta of parent page
    		update_field('gemiddelde_score_hosting_provider', $get_average_score, $get_child_page_ID);
    add_action( 'template_redirect', 'when_a_review_gets_submitted' );
    add_action('rwp_after_saving_review', 'when_a_review_gets_submitted', 11, 2);