Created
May 2, 2019 06:32
-
-
Save daronspence/894138183c7932f7e7cba84c2c726466 to your computer and use it in GitHub Desktop.
Copy last row of ACF field to hidden custom fields on save
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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