I have some custom posts that lists cars, motorcycles and trucks.
The values in the ACF fileds are automaticaly populated and I cannot change them manually because they come from an external software.
In this CPT I have 2 ACF fields (both are text fields):
one called “Categroy” that can be Cars, Motorcycle, Truck.
Another one called “Service”, that can be a value from 1 to 9.
As you can see the “objects” can have different Categories but they maybe share the same “Service”.
For example:
Categroy = Car Service = 9
Category = Truck Service = 9
I would like to update the value of the ACF Field “Service” based on the value in “Category”.
For example:
if Category is “Car” and the Service is 9, then 9 should be udpated in “Rent”
if Categroy is “Truck” and Service is 9, then 9 should be udpated in “Buy”
How can I achieve that?
CodePudding user response:
Hello You can Use The below Code
function UpdateServiceField($post_id){
$category_field = get_field('category',$post_id);
$service_field = get_field('service',$post_id);
if ($category_field ==9) {
update_field('service','9',$post_id);
}
}
add_action('acf/save_post', 'UpdateServiceField', 20);
CodePudding user response:
Here is a more complete example . you want to change the content of some fields based on the selection of a combo box. Here the JS part : jQuery(document).ready(($) => { acf.addFilter('select2_ajax_data',(data, args, $input, field, instance) => {
// Get the combo field instance
let your_combo = acf.getFields({
name: 'your_combo_field_name'
})[0];
if (your_combo !== undefined && (data.field_key === field.get('key')) {
data.your_value = your_combo.val(); // Here is the magic ;)
}
}
});
})