Support

Account

Home Forums General Issues Storing repeater values as one single array in the database

Solving

Storing repeater values as one single array in the database

  • I have a complex repeater with, for each row, a dozen of subfields in different groups.
    Even if a post usually don’t have that many rows (less than 10), that’s still hundreds of values added in the database for each post, which is starting to hurt performance on the admin side.

    So I’m wondering: is it possible to have all the repeater values stored as one array in the database? The same array that is generated when doing get_field(‘my_repeater’)

  • I could always generate the array after save and then delete the repeater, but it seems a bit messy :

    add_action('save_post', 'save_repeater_as_array');
    function save_repeater_as_array($post_id) {
    	$repeater = get_field('my_repeater', $post_id);
    	$json = json_encode($repeater);
    	update_field('json',$json,$post_id);
    	
    	if (!empty($repeater)) {
    		while (delete_row('my_repeater', 1, $post_id));
    	}
    }

    Also, I’ll have to rebuild the repeater on page load with JS, but that’s okay I don’t mind putting in the work.

  • Yes, it’s possible to store all repeater values as one array in the database. One approach is to serialize the repeater data into a single array before storing it in the database. This way, you’ll only have one database field storing all the repeater values for each post. When retrieving the data from the database, you can unserialize the array to access the individual values.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.