Support

Account

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

Solving

Save/update field of custom field with function

  • Maybe somebody on this forum can help me out because I am lost and even thinking what I am trying to achieve is not possible at all:

    Get post-meta ‘rwp_user_score’ and load it in to acf ‘score_provider’
    function my_acf_load_field( $field ) {
    global $post;
    if ( 0 !== (int) $post->post_parent )
    {
    $some_value = get_post_meta( $post->post_parent, ‘rwp_user_score’, true );
    if ( ! empty ( $some_value ) )
    switch ( $field[‘name’] ) {
    case ‘score_provider’:
    $field[‘value’] = ” . $some_value .”;
    break;
    }
    }
    return $field;
    }

    add_filter(‘acf/load_field’, ‘my_acf_load_field’);

    This action is triggered when a user submited a new review on the front-end (on the page that stores the meta ‘rwp_user_score’)
    function rex_my_action ($review, $post_id) {
    // Build your action after the review is saved.
    add_action(‘acf/save_post’, ‘move_image_to_post_thumbnail’);
    }
    add_action(‘rwp_after_saving_review’, ‘rex_my_action’, 11, 2);

    This function should update the value in ‘score_provider’ with the updated meta ‘rwp_user_score’
    function move_image_to_post_thumbnail($post_id) {
    if (get_field(‘score_provider’, $post_id, false)) {
    // get unformatted value
    // we did it above too, no point formatting the value for this
    $image_id = get_field(‘score_provider’, $post_id, false);
    update_post_meta($post_id, ‘rwp_user_score’, $image_id);
    }
    }

    I added the action of above function to the function (rex_my_action) that gets triggered after a review is submited.

    I found the above function on the support of advanced custom fields:
    https://support.advancedcustomfields.com/forums/topic/get-featured-image-and-save-as-field/

    So what I am basicly trying to achieve is that when a user submit a new review the new saved meta (rwp_user_score) of the parent pages is saved again inside the ACF score_provider. So it gets updated when somebody posts a new review.

    I know ACF basicly works that fields gets updated only when the page where the fields are on is saved again.

    What am I doing wrong and how can I manage to get it to work? Is it even possible to do this?

  • Ok, I got it working but I think with this function he is checking every parent page with his childeren or not?

    Is there a way to only trigger everything for the parent and his childeren on the page where the review is submitted?

    This is the function I currently have:

    function rex_my_action ($review, $post_id) {
    	
    // Set up the objects needed
    $hosting_provider_query = new WP_Query();
    global $post;
    $hosting_pagina_titel = $post->post_title;  
    $doorzoek_alle_paginas = $hosting_provider_query->query(array(
        'post_type' => 'page',
        'post_status' => 'publish',
        'meta_query' => array(
    		'relation' => 'AND',
            array(
                'key' => 'naam_hosting_provivder',
                'value' => $hosting_pagina_titel,
    			'compare'	=> '='
            ),
    
        ),
    ));
    	
    // Filter through all pages and find Page's children
    $alle_child_paginas_doorlopen = get_page_children( get_the_ID(), $doorzoek_alle_paginas );
    	
    // echo what we get back from WP
    if(!empty($alle_child_paginas_doorlopen)){
        foreach($alle_child_paginas_doorlopen as $pakket_hp){
    		$id_tonen = $pakket_hp->ID;
    		$postid = get_queried_object_id();
    		$some_value = get_post_meta( $postid, 'rwp_user_score', true );
    		update_field('gemiddelde_score_hosting_provider', $some_value, $id_tonen);
    	}
    }
    	
    }
    add_action( 'template_redirect', 'rex_my_action' );
    add_action('rwp_after_saving_review', 'rex_my_action', 11, 2);
  • If all you are trying to do is update a parent post when the post is saved then something like this should do it. Please note that this may not be exactly the code and it’s just an outline.

    
    add_action('acf/save_post', 'some_function_name_here', 20);
    function some_function_name_here($post_id) {
      // get the value from the saved post that you want to use
      $value = get_field('field_name', $post_id);
      if (!$value) {
        // no value, no need to continue
        return;
      }
      $parent = wp_get_post_parent_id($post_id);
      if (!$parent) {
        // not parent post, bail
        return;
      }
      // use update_field to update the value on the parent post
      update_field('field-name-on-parent-post', $parent, $value);
    }
    
  • Hello John,

    Thanks for your answer but this is not what I meant.

    The meta value (rwp_user_score) of the parent page is updated when this function is being executed. When a user submits a new review this value is updated automaticly.

    Only the custom field (gemiddelde_score_hosting_provider) of all the child pages of the parent are getting the value from the parents meta value (rwp_user_score). This value gets stored in the custom field (gemiddelde_score_hosting_provider).

    The current function is doing this:

    – Get all pages where custom field value of naam_hosting_provivder equals the post_title
    – Then get all childs of this pages
    $alle_child_paginas_doorlopen = get_page_children( get_the_ID(), $doorzoek_alle_paginas );
    – in the for each loop store every child page ID in a variable
    $id_tonen = $pakket_hp->ID;
    – then get the Page ID of the parent
    $postid = get_queried_object_id();
    – then get the meta ‘rwp_user_score’ of parent page (ID)
    $some_value = get_post_meta( $postid, 'rwp_user_score', true );
    – and finaly update the field where it is al about
    update_field('gemiddelde_score_hosting_provider', $some_value, $id_tonen);

    My coding skills are not that great but I think this function is looping through every page and its childs. When the site goes online we expect to have over 100 parent pages and each parent has over 10 child pages.

    So I think it is a bit of an overkill to loop and update the field (gemiddelde_score_hosting_provider) of every child page if only one meta value of a parent gets updated.

    Better explanation

    While I am explaining this, I can clearly understand that it is not very clear what I am trying to achieve. Maybe it’s useful if I write it off completely.

    On a page, a user can leave a review. The average score is stored in the post_meta under ‘rwp_user_score’ of this page.

    Each page represents a hosting provider and each hosting provider has packages. Each package is a child of a hosting provider page and has a custom field called “gemiddelde_score_hosting_provider”.

    The average score stored in the post_meta of the hosting provider page I put in the custom field “gemiddelde_score_hosting_provider” for each package of this hosting provider.

    This is all done when a new review submitted.

    Only with the current function, all custom fields of the child pages of each parent page are updated again.

    This is a bit redundant as he only has to update the custom fields of the child pages of the parent page where a review is submitted at that moment.

    Is it possible to realize this with my current function?

  • I’ve read over this several times and I’m going to be honest, I don’t really understand what you’re trying to do from reading your description.

    Looking at the code, I can’t figure out what the code is actually accomplishing, I don’t even see how it can be accomplishing what you think it should be, but this is probably due to my lack of understanding of what you’re trying to do.

  • 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.

    Code:

    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',
            array(
                '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
    if(!empty($loop_through_all_child_pages)){
        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);

    Image:
    https://imgur.com/a/l7JfYSa

  • So, after looking at the image, I have a question.

    Every hosting package has a custom field named …. I want to show the average score of the hosting provider on every hosting package ….

    Is there any purpose for this field to exist on the child pages other than displaying that value on the front of the site? Or using that value on the front of the site?

  • Hello John,

    Thanks for your reply.

    Yes that custom field in all the child pages is the purpose of this whole thing.

    Every child page stands for a hosting package of a provider. Every provider has a average score that is pulled of the meta of the Provider page and put in the custom field of the child.

    Why? I’m using FacetWP for filtering through those packages and I need the average score of each hosting provider package to make the star rating filter to work.

  • So there is another purpose other than display.

    So here’s the thing. If this is an old site then you need something that does what you want done, that is read through existing posts and update values.

    But if this is a new site, I think that it may be overcompensated.

    Let me see if I understand this correctly.

    1) There is a parent page for a “Provider”
    2) People can submit reviews for this “Provider”
    3) Somehow the “Provider” page is updated with the average of all reviews
    4) Each “Provider” has multiple “Packages”
    5) When the average number on the “Provider” is updated then this average needs to be added to all “Packages” from that provider.

    If this is correct.

    Where, when, and how is the “Average” rating updated on the “Provider” post?

  • Hello John,

    That is exactly what it is.

    Where and how the “Average” rating is updated on the Provider post I dont know. This is a plug-in I’m using.

    I know the answer on the When, when a new review gets submitted on the Provider post.

    With this function:

    function when_a_review_gets_submitted ($review, $post_id) {
    // Do something
    }
    
    add_action('rwp_after_saving_review', 'when_a_review_gets_submitted', 11, 2);

    I can do something after a new review is submited.

    I want to do this:

    // 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',
            array(
                '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
    if(!empty($loop_through_all_child_pages)){
        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);
        }
    }

    I once got it working when adding the above script inside the function with this extra action:

    add_action( 'template_redirect', 'when_a_review_gets_submitted' );

    but this is causing an error now and nothing is getting updated.

    Error

    Warning: Missing argument 2 for when_a_review_gets_submitted(), called in /var/www/vhosts/domainname.nl/httpdocs/wp-includes/class-wp-hook.php on line 286 and defined in /var/www/vhosts/domainname.nl/httpdocs/wp-content/themes/hosting-vergelijker/functions.php on line 452

    You have any idea how to get this done?

  • Sorry I haven’t gotten back to you sooner.

    I’m not sure I understand why you are doing

    
    add_action( 'template_redirect', 'when_a_review_gets_submitted' );
    

    instead of

    
    add_action('rwp_after_saving_review', 'when_a_review_gets_submitted', 11, 2);
    
  • Because when I use the rwp_after_saving_review nothing gets updated. The page doesnt refresh so for some reason no field updates are done on the childs of this provider the review is left behind…

    Template_redirect does work for some reason but that refreshes (updates) the fields of all childs and not just the fields of the childs where a user left a review.

    But never mind, I think it is to complicated for me to get this to work properly

    Thanks anyway

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

The topic ‘Save/update field of custom field with function’ is closed to new replies.