Support

Account

Home Forums General Issues How to display highest number value in post field by author name?

Solving

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.

  • Hi @mswas

    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
    E

  • 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.

  • Hi @mswas

    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.

    Thanks
    E

  • Can the review number custom field that I have now update the user field? Or must they log into their profile to update it.

  • Hi @mswas

    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().

    Good luck

    Cheers
    E

  • 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)

  • Hi @mswas,

    Try changing:

    $value = get_field($cbr6_review_number);

    to

    $value = get_field("cbr6_review_number");

    Cheers

  • Hi @mswas

    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 acf/update_value filter.
    3. Your 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.

    Thanks
    E

  • 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);
    
Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘How to display highest number value in post field by author name?’ is closed to new replies.