Support

Account

Home Forums General Issues ACF to manage data in custom database tables Reply To: ACF to manage data in custom database tables

  • acf frontend form itself has validation before its submitted. use acf/save_post filter to fire an additional action to save the data in your custom Database.
    it would be stored in default custom fields also though..

    i would do it something like:

    setup database table ‘custom_events’ beforehand ofc

    function insert_event(array $args = []) {
        
        $defaults = [
            'event_name' => null,
            'time' => date('Y-m-d H:i:s'),
        ];
    
        $values = wp_parse_args($args, $defaults);
    
        if ( is_null($values['event_name']) ) {
            return new \WP_Error("Please provide an event name");
        }
    
        global $wpdb;
        $table_name = 'custom_events';
        $table_name = $wpdb->prefix . $table_name;
    
        $wpdb->insert($table_name, $values);
    
        return $wpdb->insert_id;
    }
    
    add_action('yourprefix/events/insert', 'insert_event' );
    
    function my_acf_save_post( $post_id ) {
    
        // Get newly saved values.
        $values = get_fields( $post_id );
    
        // Make sure $values acf fields and your custom database fields match their names
        do_action('yourprefix/events/insert', [ $values ]);
    
    }
    add_action('acf/save_post', 'my_acf_save_post');

    you would need an update action aswell and also error handling its just the direction i would to it.

    infact i do it same on some projects i maintain WP Post Data but additional data in custom tables