Last active
June 5, 2025 11:03
-
-
Save vishalkakadiya/7db353a9c27c49571cc92b2764bc8d2c to your computer and use it in GitHub Desktop.
Save ACF repeater fields in serialized format to save meta rows
This file contains hidden or 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 | |
/** | |
* This is example code, not directly use on the Production site. | |
*/ | |
add_action( 'acf/save_post', function ( $post_id ) { | |
// Skip options page and revisions/autosaves. | |
if ( $post_id === 'options' || wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { | |
return; | |
} | |
// Field key for pi_repeater_slides. | |
$field_key = 'field_68416c37f6cf4'; | |
$field_name = 'pi_repeater_slides'; | |
if ( ! isset( $_POST['acf'][ $field_key ] ) ) { | |
return; | |
} | |
$slides_data = $_POST['acf'][ $field_key ]; | |
// Save serialized array in a single meta key. | |
update_post_meta( $post_id, "serialized_{$field_name}", maybe_serialize( $slides_data ) ); | |
global $wpdb; | |
// Delete all post meta keys related to ACF's default repeater format. | |
$meta_key_like = $wpdb->esc_like( $field_name ) . '%'; | |
$wpdb->query( | |
$wpdb->prepare( | |
"DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND ( meta_key LIKE %s OR meta_key LIKE %s )", | |
$post_id, | |
$meta_key_like, | |
'_' . $meta_key_like | |
) | |
); | |
}, 20 ); | |
add_filter( 'acf/load_value/name=pi_repeater_slides', function ( $value, $post_id, $field ) { | |
$serialized = get_post_meta( $post_id, 'serialized_pi_repeater_slides', true ); | |
return $serialized ? maybe_unserialize( $serialized ) : []; | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment