Hello,
How can i freeze fields in the fist time or previous i called them?
I’m writing a plugin which use fields for calculation something of my job. The plugin will output and add results to WP database. Because there are many functions for output results, i just want to call them one time if the fields were not updated.
But have a problem. If the fields were updated through editing post, i need to know for call back some functions again and update some to database. any ways can catch data of previous time i called get_fields()
to compare with newest get_fields()
(in a post)?
the acf/save_post hook can be used https://www.advancedcustomfields.com/resources/acf-save_post/. With a priority < 10 your action will run before new values are saved. You can get the current values of the fields using things like get_field()
. You can then look at the values submitted by looking in $_POST['acf']
or you can have another action run with a priority > 10 if you wan to use acf functions like get_field()
.
Hello John,
I read the link but i do not really understand. My English is not good and i work with WP Just recently.
I try some like this:
function my_acf_save_post( $post_id ) {
// array of field values
$fields = $_POST['acf'];
// specific field value
$field = $_POST['acf']['luc_nham']; // 'luc_nham' is an array fields i need to get
}
add_action('acf/save_post', 'my_acf_save_post', 1);
function tess_fields()
{
$id = get_the_ID();
$fields = my_acf_save_post($id);
var_dump($fields);
}
add_shortcode('tess', 'tess_fields');
Then i try to edit some fileds in a post, but alway return null. I dont know exactly i need to do. Can you help me an clearn example.
Many thanks!
from you original post, what you want to do is detect if certain fields have changed, and if they have to run some code.
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post($post_id) {
// you need the field key of the field you want to check
$field_to_check_key = 'field_012345678';
$old_value = get_field('my_field_to_check', $post_id, false);
// see if it is different than new value
// also make sure the field key is set
if (isset($_POST['acf'][$field_to_check]) &&
$old_value != $_POST['acf'][$field_to_check]) {
// value has change
// call function to do calculations
}
}
Hello John,
I access to the dababase and find in column meta_value, i seem something like field_585d2d2af8d69. Are they field key?
Then i try edit your code and run test:
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post()
{
// you need the field key of the field you want to check
$field_to_check_key = 'field_585d2d2af8d69';
var_dump($field_to_check_key);
$old_value = get_field('luc_nham_0_can', 339, false); // 339 is curent post ID
var_dump($old_value);
// see if it is different than new value
// also make sure the field key is set
if (isset($_POST['acf'][$field_to_check_key]) &&
$old_value != $_POST['acf'][$field_to_check_key])
{
echo 'Hello the Wolrd'; // Test if the field has changed.
}
// Test current fields
$curent_field = get_field('luc_nham_0_can');
var_dump($curent_field);
}
// Shortcode to test
add_shortcode('tess', 'my_acf_save_post');
I try to change the testing field many time, but $old_value has updated same $curent_field, it not keep old value.
You can see some images in this comment.
gonna be honest, not sure what you’re doing there, so here’s another stab code to explain what I was doing.
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post($post_id) {
// you need the field key of the field you want to check
$field_to_check_key = 'field_585d2d2af8d69';
// see if this field is set
if (isset($_POST['acf'][$field_to_check_key])) {
// this field is not submitted, must be a different group
return;
}
// the new value is submitted
$new_value = $_POST['acf'][$field_to_check_key];
// get the old value
$old_value = get_field('luc_nham_0_can', $post_id, false);
// test new value to see if it's different than old value
if ($new_value != $old_value) {
// it is different
// do something
}
}
Hi John,
i read about WP core and i found the way to help me do it.
Thank you for help!
The topic ‘Previous and next get fileds’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.