Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy last row of ACF field to hidden custom fields on save
<?php
/**
* Plugin Name: ACF Copy Repeater Field to Singular - docs_list_renew
* Version: 0.1
* Description: Copies the values of the last row of a repeater field to separate custom fields to be used with Admin Columns
*/
// Rename the second argument if you're going to copy the plugin to work with another repeater.
add_action('wp_insert_post', 'reddit_acf_copy_repeater_field_to_singular', 10);
// Rename this function if you're going to copy the plugin to work with another repeater.
function reddit_acf_copy_repeater_field_to_singular($id){
$repeater_name = "docs_list_renew";
if ( ! function_exists('get_field') ){
// ACF is not installed.
return;
}
$repeater = get_field($repeater_name, $id);
if ( !$repeater ){
// repeater was not found
return;
}
$last_row = end(get_field($repeater_name, $id));
if ( empty($last_row) ){
// repeater is empty
return;
}
foreach ($last_row as $key => $value){
if ($value !== ''){
update_post_meta($id, '_' . $key . '_automated_copy', $value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.