Home › Forums › General Issues › How to display highest number value in post field by author name?
I have a book review blog site with multiple authors.
Rules allow multiple reviews to be written in one post, so we can’t use the # of posts written to count how many books the author has read.
I created an ACF number field on the post that the author fills out when posting the review where they self-report which number review it is. If the review contains multiple books, they enter the highest number in the field.
I want to display the review # next to the user name, so we can have a page showing the standings. Did I create this field incorrectly? Should this post field somehow update a field in the user profile instead? Would that be through a relationship field? I’m stumped as to how to proceed.
Before I can give you an answer to this, I will need to better understand your data setup. Can you please take a screenshots of all post edit screens in question?
Also, what do you mean by
I want to display the review # next to the user name? Where is the user name and what is the logic (in english) to relate the username to the review number you want to load.
Thanks for the help. I’ve uploaded screenshots here: http://imgur.com/a/Uzcr3
The review number I am showing on the book review itself, see last screenshot, but I would like to use it in other ways.
1 – to show who’s in first place so far in the “race”. So what is the highest number in that field, and who is that author. (1st, 2nd, 3rd would be good too)
2 – to show all of the authors alphabetically with their highest review number next to them.
3 – to show all of the authors listed in order by highest review number.
Thanks for the screenshots. To achieve the 3 questions above, you will need to add a field to the user. This field will contain the ‘reviews’ number.
For now, you can ask your editors to manually edit this number when they post a new review (edit profile, update custom field and save profile). You could also write some code to do this automatically, but lets talk about that later (after you have achieved 1,2,3 above).
With each user now containing a custom field for the ‘reviews’ number, you can use the WP_User_Query to query the users and display them in a list.
You can also output the user custom field (checkout the docs for more info on users) and sort them based on the custom field value based on teh custom field value!
This should solve all your problems!
basically, it won’t be possible to achieve the above with your current data setup and the user custom field is needed.
Can the review number custom field that I have now update the user field? Or must they log into their profile to update it.
Yes. Please read up on the
acf/update_value filter. You can use this filter (use the field key to restrict your code to only run on the specific field) to run some custom code when the value is updated.
Your custom code could take the value, and just update the current users custom field value – use update_field().
This helped! Now I am trying the acf/update_value.
This is the code I have in my functions file:
///field update $field_name = "cbr6_review_total"; $value = get_field($cbr6_review_number); update_field( $field_name, $value );
But it doesn’t update the review total field (the user field) with the number from the review number field (on the post)
$value = get_field($cbr6_review_number);
$value = get_field("cbr6_review_number");
There are few issues with your code:
1. As mentioned by @ACF Support, you should use $field_name in the get_field function.
2. Make sure you are loading from the correct post by adding in the $post_id parameter to the
get_field function. This parameter is given to you in the
update_field function is not targeting the user. You need to find the current user (with some WP code) and use that ID to target the save function.
Coming back to this and still having trouble… This is my current code:
$author_id = get_the_author_meta( 'ID' ); $field_name = get_field("cbr7_review_total", $author_id); $value = get_field('$cbr7_review_number', $post->ID); update_field( $field_name, $value, $author_id);
The topic ‘How to display highest number value in post field by author name?’ 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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.