eduardo-marcolino / acf-fields-in-custom-table

Stores ACF custom fields in a custom table instead of WordPress' core meta tables
27 stars 12 forks source link

API is not reflected #18

Open Nowi5 opened 2 years ago

Nowi5 commented 2 years ago

Hi,

I faced an issue, that when I use ACF via Rest API (https://github.com/airesvsg/acf-to-rest-api) I get a 200 but the new values are not reflected. 3 hours later I spotted the issue, that via the API the acf fields in custom table events are not triggered and no values were visible in the database. So here is the solution:

acf-fields-in-custom-table.php - add a new filter - line ~80 add_filter( 'acf/update_value', [$this, 'update_field_in_custom_table'], 11, 3 );

Add new function at line ~181:

public function update_field_in_custom_table($value, $post_id, $field){     
         global $wpdb;

        if($field["readonly"]){
            return;
        }

        if (
          $field[self::SETTINGS_ENABLED] && $field['name'] &&
          $this->is_supported($field)
        ){      
            $column_name    = acfict_sanitize_keyword($field['name']);
            $table_name     = $this->table_name($field[self::SETTINGS_TABLE_NAME], $field[self::SETTINGS_USE_PREFIX]);
            $dbvalue        = apply_filters('acfict_sanitize_'.$field['type'],$value,$field);

            $wpdb->update($table_name, [$column_name => $dbvalue], ['post_id' => $post_id]);        
        }

        return $value;
    }

See https://www.advancedcustomfields.com/resources/acf-update_value/ for more information on this filter.

Please be aware, that with this issue, we add the entries to the table in addition to the meta table. If you have an alternative solution, please let me know.

Thank you.

Nowi5 commented 2 years ago

Updated a bit, as new entries need beside an update also an insert:

    public function update_field_in_custom_table($value, $post_id, $field){     
         global $wpdb;

        if($field["readonly"]){
            return;
        }

        if (
          $field[self::SETTINGS_ENABLED] && $field['name'] &&
          $this->is_supported($field)
        ){      
            $column_name = acfict_sanitize_keyword($field['name']);
            $table_name = $this->table_name($field[self::SETTINGS_TABLE_NAME], $field[self::SETTINGS_USE_PREFIX]);
            $dbvalue = apply_filters('acfict_sanitize_'.$field['type'],$value,$field);
            $data = ['post_id' => $post_id, $column_name => $dbvalue];

            $sql = "INSERT INTO $table_name (post_id,$column_name) VALUES (%d,%s) ON DUPLICATE KEY UPDATE $column_name = %s";
            $sql = $wpdb->prepare($sql,$post_id,$dbvalue,$dbvalue);         

            $wpdb->suppress_errors = true;
            $wpdb->show_errors = false;

            if ( false  === $wpdb->query($sql)){
                $message = __('ACF: Fields in Custom Table error:', 'acfict').$wpdb->last_error;
                acfict_admin_notice_add($message, 'error');
            }

        }

        return $value;
    }