johannheyne / advanced-custom-fields-table-field

Advanced Custom Fields: Table Field
15 stars 4 forks source link

update_field for option is not working #10

Closed staurand closed 6 months ago

staurand commented 4 years ago

Hello,

I'm trying to programmatically update a table saved as an option but there is a condition that does not allow me to save it in advanced-custom-fields-table-field/acf-table-v5.php (l.442): $data = get_post_meta( $post_id, $field['name'], true ); // prevents updating a field, thats data are not defined yet if ( empty( $data ) ) { return false; } Because it's an option get_post_meta always returns false. Could we change "get_post_meta" to an ACF function like get_field? (so saving an option would work)

Many thanks.

Tazintosh commented 4 years ago

Hi, I do have this issue. Lost hours pulling my hairs trying to figure out what was going on. Here's the email I wrote to Johann today:

Hi Johann, I'm probably missing something here, but must be tired cause I cannot find it. I've wrote this basic code to test out how to update a table on ACF thanks to your work: For some reason, when this code runs and I check the database, my field value is set to blank; any thought? Thanks a lot in advance!

$tableData = [
    'header' => [
        ['c' => 'My Header Name']
    ],
    'caption' => [],
    'body' => [
        [ // Column
            [ // Cell
                'c' => 'Cell Value of Column 1'
            ]
        ]
    ]
];
update_field('test', $tableData, 'option');

Did you found a solution @staurand?

Tazintosh commented 4 years ago

Ok, sadly by the lack of developper answer since this topic have been opened, I made my workaround, hope it will help others. Change line 442 of acf-table-v5.php by the following:

if (get_field_object($field['name'], 'option')){
    $data = get_field($field['name'], 'option', false);
} else {
    $data = get_post_meta( $post_id, $field['name'], true );
}
staurand commented 4 years ago

@Tazintosh I've replaced l. 442 too by : $data = acf_get_metadata($post_id, $field['name']);

rrabillo commented 2 years ago

More than a year old, but had the same issue. To be more specific, i'm creating multiple posts and and importing csv values in the tables.

As far as i understand, the plugin assume the table field to already have a value. You can simply use add_post_meta() before using update_field().

It's a solution if you don't want to edit the plugin's files.

add_post_meta($id, 'table_data', 'whatever', true);
update_field('table_data', $your_value, $id);

Update: Also, it seems that only string values are accepted, so always "c"=>$string, otherwise, the backend throw a JS error.

louis-lau commented 1 year ago

The above workaround with 'whatever' doesn't work on PHP 8. This does work however:

add_post_meta($id, 'table_data', ['c'=>[]], true);
update_field('table_data', $your_value, $id);
thomstratton commented 1 year ago

see also https://wordpress.org/support/topic/table-field-on-an-options-page/