Firstly I just wanted to thank you for creating such a fantastic plugin.
I’ve been trying to get to the bottom of this issue for hours now but I’m having to admit defeat.
Basically I’m having a issue with update_field I have a custom taxonomy (created through the CRT UI plugin) called company. Then via ACF I’ve created a custom field called average_review_score. What I’m trying to do is update this field through a fuction that used a mysql query to give me the average. I can get the data I need and I can appear to update the field. However in wp-admin when I view the custom taxonomy it is still showing the old value. I know I must be missing something but cannot figure what. This is my dummy code that I’ve been testing with:
$field_key = "average_review_score"; $value = "8"; $post_id = "3"; $get_av = get_field( "average_review_score", $post_id ); echo $get_av; update_field( $field_key, $value, $post_id );
If I echo $get_av I get the result I’ve set in $value.
Am I missing something?
First, you’re using
$value whenever you update the field, so it will always be 8 from the code you have here. Second, you say that you have a custom taxonomy and you’ve put the field on the taxonomy, but you’re not using a
$post_id that is for a custom taxonomy. Overall, the explanation of what you’re doing isn’t very clear. Can you give more information about what you’re trying to do?
Sorry I was trying to keep the code simple to post on here. Yes, sorry I understand that the
$value will always be 8. This value eventually be drawn from a mysql query so will show between 1 – 10 depending on the result.
What I have is a custom taxonomy field which I wanted to store the average review score of all the customer reviews related to that taxonomy. So for example if the company’s average score was 8 then it would update my average_review_score field. I can then draw on this value for other parts of the site. This will be updated whenever I call a function that I have written.
In the function it draws the data via a mysql query. From what I understand I then need to use
update_field( $field_key, $value, $post_id ); to update the field. This appears to work because if I execute the following code
$get_av = get_field( "average_review_score", $post_id ); echo $get_av;
it has changed the value to whatever I enter.
However when I view the Edit Tag in wp-admin its still showing the old value that I originally entered:
I must admit I might have totally misunderstood what I can and cannot do when updating the field. But I basically want to update the value, both in the frontend and backend of the site.
Hopefully this makes it a little clearer as to what I’m trying to accomplish.
Looking at your original code, see my comments in the code
$field_key = "average_review_score"; $value = "8"; $post_id = "3"; $get_av = get_field( "average_review_score", $post_id ); echo $get_av; // this is updating the post with an ID of 3 // it is not updating a term in a taxonomy update_field( $field_key, $value, $post_id );
If your intent is to update a field that is attached to a term in a taxonomy then you need to use a $post_id that represents that term. See “Getting values from other places” on this page https://www.advancedcustomfields.com/resources/get_field/
You must be logged in to reply to this topic.
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!
© 2023 Advanced Custom Fields.