How can I update an ACF field in a repeater on post save?

admin2025-01-07  3

Using Advanced Custom Fields, I have a repeater field (domain_assets), within which I have two text fields (registered_domain & ssl_expiration), and a sub repeater field (service). I'm trying to dynamically set the 'ssl_expiration' text field any time the post is saved using a combination of the hook 'acf/save_post' along with the 'update_row()' function, but it doesn't seem to be working for me.

This is what I have...

add_action('acf/save_post', 'set_domain_ssl_expiration');
function set_domain_ssl_expiration($post_id){
    if (have_rows('domain_assets')) :
        while( have_rows('domain_assets') ) : the_row();
            $domain = get_sub_field('registered_domain');
            if (!empty($domain)) :
                  update_row('ssl_expiration', 3, get_ssl_info($domain, 'validTo_time_t'), $post_id);
            endif;
          endwhile;
    endif;
}

I'm getting the new value of the ssl_expiration field from a function named get_ssl_info() which I'm not including here. It uses cURL to read the SSL certificate information and return data based on a given parameter.

When I print the values of the variables '$post_id' and '$domain', along with the output of the function "get_ssl_info($domain, 'validTo_time_t')" and send them to the error_log within the hook's callback function, I get the correct values returned for each. Even when I have multiple domains saved in a post, the correct values for each of the variables are sent to the error_log for each corresponding domain.

For good measure, I decided to add a statement to print the output of 'get_rows()', immediately after the while statement and confirmed that, sure enough, each of the 'domain_assets' repeater rows are printed, including the empty ssl_expiration field. Given the fact that everything else works as it should, I feel like it is something to do with the 'update_row()' function, but I don't see what the problem is.

I'm really not sure what to try at this point. Regardless of whether I save a new custom post or update an existing one, the 'ssl_expiration' text field still remains empty. What I am missing?

Using Advanced Custom Fields, I have a repeater field (domain_assets), within which I have two text fields (registered_domain & ssl_expiration), and a sub repeater field (service). I'm trying to dynamically set the 'ssl_expiration' text field any time the post is saved using a combination of the hook 'acf/save_post' along with the 'update_row()' function, but it doesn't seem to be working for me.

This is what I have...

add_action('acf/save_post', 'set_domain_ssl_expiration');
function set_domain_ssl_expiration($post_id){
    if (have_rows('domain_assets')) :
        while( have_rows('domain_assets') ) : the_row();
            $domain = get_sub_field('registered_domain');
            if (!empty($domain)) :
                  update_row('ssl_expiration', 3, get_ssl_info($domain, 'validTo_time_t'), $post_id);
            endif;
          endwhile;
    endif;
}

I'm getting the new value of the ssl_expiration field from a function named get_ssl_info() which I'm not including here. It uses cURL to read the SSL certificate information and return data based on a given parameter.

When I print the values of the variables '$post_id' and '$domain', along with the output of the function "get_ssl_info($domain, 'validTo_time_t')" and send them to the error_log within the hook's callback function, I get the correct values returned for each. Even when I have multiple domains saved in a post, the correct values for each of the variables are sent to the error_log for each corresponding domain.

For good measure, I decided to add a statement to print the output of 'get_rows()', immediately after the while statement and confirmed that, sure enough, each of the 'domain_assets' repeater rows are printed, including the empty ssl_expiration field. Given the fact that everything else works as it should, I feel like it is something to do with the 'update_row()' function, but I don't see what the problem is.

I'm really not sure what to try at this point. Regardless of whether I save a new custom post or update an existing one, the 'ssl_expiration' text field still remains empty. What I am missing?

Share Improve this question edited Nov 28, 2022 at 19:44 soupmagnet asked Nov 28, 2022 at 15:52 soupmagnetsoupmagnet 1436 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Please try this is working for me:

$value[] = array( 
    //field data-key
    'field_6433b7964ee2b' => 'image',    
    //'field_644b7312b7e8f' => 'https://vimeo.com/76979871000',
    'field_6433b7bb4ee2c' => '301'
);

$value[] = array( 
    'field_6433b7964ee2b' => 'vimeo',
    'field_644b7312b7e8f' => 'https://vimeo.com/76979871000',
);

//field data-key     
update_field( 'field_6433b77b4ee2a', $value, $new_page_id );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261558a750.html

最新回复(0)