最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

advanced custom fields - ACF not updating value correctly

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far