Learn how to convert a repeater field named "colors" in PHP to a standard WP meta field, allowing for easier search, sort, and filter functionality.
<?php
add_filter('acf/save_post', 'convert_color_to_standard_wp_meta', 20);
function convert_color_to_standard_wp_meta($post_id) {
$meta_key = 'color_wp';
delete_post_meta($post_id, $meta_key);
$saved_values = array();
if (have_rows('colors', $post_id)) {
while (have_rows('colors', $post_id)) {
the_row();
$color = get_sub_field('color');
if (isset($saved_values[$color])) {
continue;
}
add_post_meta($post_id, $meta_key, $color, false);
$saved_values[$color] = $color;
}
}
}