advanced custom fields - ACF not updating value correctly

admin2025-06-04  1

I'm creating a backend page that shows off some stats from projects i have included inside my wordpress site.

The basic is pretty straight forward:

js that sends out an ajax request (action) that gets intercepted by wordpress to interact with it's database.

This is working fine.

The issue i've got is that i am not able to correctly get/set the value for ACF.

I have a very basic function that looks like this:

function update_projects_settings(){
    $request_body = file_get_contents('php://input');
    $fieldRequest = $_REQUEST['field'];
    $valueRequest = $_REQUEST['value'];
    $newValue = 0;

    $group_ID = 16267;
    $fields = array();
    $fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);
    if ($fields) {
        foreach ($fields as $field) {
            if ($field['name'] === $fieldRequest){
                $previousValue = get_field($field['name']);
                $defaultValue = $field['default_value'];
                $frontendValue = $previousValue ? $previousValue : $defaultValue;
                $newValue = $frontendValue + $valueRequest;
                update_field($field['name'], $newValue, $group_ID);
            }
        }
    }
}

and debugging it seem to be working correctly (it gets the right field and the right values)

and then i display those values in an admin page that calls this function here (again, pretty simple):

<?php 

$group_ID = 16267;

$fields = array();
$fields = apply_filters('acf/field_group/get_fields', $fields, $group_ID);

?>
<div class="container-fluid">
    <div class="projects-settings-section">
        <small>info</small>

        <ul class="list-group">
            <?php
                if ($fields) {
                    foreach ($fields as $field) {
                        ?>
                        <li class="list-group-item">
                            <?php
                                $value = get_field($field['name']);
                                $defaultValue = $field['default_value'];
                                $frontendValue = $value ? $value : $defaultValue;
                                echo $field['label'] . '<span class="badge">' . $frontendValue . '</span>';
                            ?>
                        </li>
                    <?php
                    }
                }
            ?>
        </ul>
    </div>
</div>

but the data retrivid from here is not correct.

I don't seem to get the correct values out of the db, and inspecting such dd here's what i get:

the weird thing about it is that the printed result on my page says the video start count is 19 and not 1 (picture below):

according to the ACF documentation i'm doing everything correctly here:

/ /

but still it doesn't work.

Any help would be really appreciated.

Thanks a lot

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749000697a315509.html

最新回复(0)