Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

using update_user_meta to rank users

It looks like the plugin is using the update_post_meta function, which in turn uses the update_metadata function which includes the action hooks update_postmeta and updated_postmeta which fire immediately before and after the post meta is stored to the database. You could hook into one of these to update the user meta of the author of the post. Pseudo code:

add_action( 'update_postmeta', 'wpse155265_update_user_vote', 10, 4 );
function wpse155265_update_user_vote( $meta_id, $object_id, $meta_key, $meta_value ) {
  $post = get_post( $object_id );
  $user_id = $post->post_author;
  $votes = (int) get_user_meta( $user_id, 'epicredvote', true );
  $votes++;
  update_user_meta( $user_id, 'epicredvote', $votes );
}

Edit:

As noted, the above does not take into consideration whether the post meta update is a vote up or down. Revised code below; we’ll compare the value of the post meta, old versus new, to determine if the user’s meta should be increased or decreased.

add_action( 'update_postmeta', 'wpse155265_update_user_vote', 10, 4 );
function wpse155265_update_user_vote( $meta_id, $object_id, $meta_key, $meta_value ) {
  $post = get_post( $object_id );
  $user_id = $post->post_author;
  $meta_value_old = get_post_meta( $object_id, 'epicredvote', true );
  $votes = (int) get_user_meta( $user_id, 'epicredvote', true );
  ( $meta_value > $meta_value_old ) ? $votes++ : $votes--;
  update_user_meta( $user_id, 'epicredvote', $votes );
}

Another approach that occurred to me just now would be to only calculate the user’s votes when ranking users, rather than storing their votes in a meta. Loop through all users from get_users and for each one, calculate their total votes, storing each user into an array with their total as the key, sort the array by vote totals using ksort and then loop through the array to output your ranked list. Running WP_Query for every user might be a bit intense, though. Perhaps there’s a single SQL statement that can be put together that will actually return all users sorted by their vote total – but I’d have to fiddle with it to see if that’s possible.

Edit:

Here’s a single SQL that will calculate and rank your users. Plug this into $wpdb as needed:

global $wpdb;
$results = $wpdb->get_results(
  "
    SELECT u.display_name as name,
    ( SELECT SUM(pm.meta_value)
      FROM wp_posts p, wp_postmeta pm
      WHERE p.post_author = u.ID
      AND p.post_status LIKE 'publish'
      AND pm.meta_key = 'epicredvote'
      AND p.ID = pm.post_id ) as votes
    FROM wp_users u
    ORDER BY votes DESC
  "
);
foreach ( $results as $result ) {
  echo "{$result->name}: {$result->votes} votes<br>";
}

Related Posts:

  1. How to display Yoast SEO meta description in archive template for each post instead of the_excerpt()? [closed]
  2. Get ID of a page containing secondary loop in content
  3. How to add “time” data this?
  4. Next and Previous loop
  5. How to hook into container
  6. Get author Meta for particular user inside the loop
  7. Can’t access PHP array inside script localization from javascript
  8. Output meta into arrays
  9. value of metadata is null wrong use of if statement
  10. How to get user ID’s from multiple usernames?
  11. Sending mail not working correctly
  12. get_post_meta printing empty fields, but it shouldn’t be
  13. How to get all multi-select user meta values and add them to an array?
  14. Check value of post meta within IF statement
  15. Exclude Posts Using Meta Query and User Meta
  16. Best way to check if a post with specific meta exists
  17. WP update_post_meta link loop
  18. Humanmade | Custom meta box class: How to display a repeatable meta box group?
  19. Display articles related to a custom field on a page
  20. Display all existing members
  21. Wishlist Icon in Divi Blog module (loop)
  22. How to fix pagination for custom loops?
  23. Jquery Slider for profile template
  24. Counting the posts of a custom WordPress loop (WP_Query)?
  25. if ( is_home() && ! is_front_page() )
  26. Get excerpt using get_the_excerpt outside a loop
  27. Get post content from outside the loop
  28. Why should I put if(have_posts()), is while(have_posts()) not enough?
  29. Display featured products through custom loop in woocommerce on template page
  30. Split Content and Gallery
  31. How to get Author ID outside the loop
  32. How can i display the content in plaintext
  33. Redirect loop when trying to login to /wp-admin/ [duplicate]
  34. How to split a loop into multiple columns
  35. Is it necessary to reset the query after using get_posts()?
  36. Is there any difference between the_title() and echo get_the_title()?
  37. AJAX with loop filtering categories
  38. Do I need to use The Loop on pages?
  39. Remove the Homepage Query
  40. remove tags from the_content
  41. the_title() shows title of the first post instead of the page title?
  42. How to force excerpts / teasers in the loop
  43. Retrieve each widget separately from a sidebar
  44. Why am I being limited to ten posts on a custom loop?
  45. Should I use loop in the single.php file?
  46. A search for ‘0’ returns results
  47. Why do themes rely on “The Loop”?
  48. How to return loop contents
  49. Using the Loop to show all levels of subpages under a parent page? Halfway there
  50. Cleanest Way to Select Every Second Element in a Loop?
  51. Can I count the number of users matching a value in a multiple value key?
  52. Content hooks vs User hooks
  53. Insert image or ad script after 3 posts using the loop
  54. Get date of last update outside of loop
  55. Pagination not working on home page
  56. Child Pages Loop
  57. How to place comments_template(); outside the loop?
  58. Multiple Loops Homepage?
  59. How to Change Loop to Order Posts by Views (using wp-postviews plugin)
  60. how to upload image using wp_handle_upload
  61. Avoiding using a loop to access a single post
  62. Strategy to get post meta for use outside the loop
  63. How to get the first image gallery of a product in woocommerce in a loop
  64. Get post by page name or slug
  65. save_post + insert_post = infinite loop
  66. Loop.php vs looping inside template file
  67. Custom Loop and Infinite Scroll
  68. Query Custom Meta Value with Increment
  69. Loop that displays PARENT PAGE & CHILD PAGE & outputs GRANDCHILD PAGE title and content
  70. is_home, and is_front_page conditional problem
  71. Is `query_posts` really slower than secondary query?
  72. Are there any scenarios where the query_posts may be used?
  73. the_content and wp_link_pages
  74. Adding ‘current_post_item’ class to current post in the loop
  75. How do I get the attributes of a short code from a post?
  76. How can I custom order the results from wp_list_categories?
  77. ACF Repeater loops and resets – where is the reset_rows() documentation? [closed]
  78. WooCommerce Product Page Loop – Output All Product Thumbnails
  79. wp-admin redirecting to https, denying login
  80. Endless loop with wp_insert_post and wp_update_post
  81. Display subpages under parent page as a list within a loop
  82. Why is it necessary to call rewind_posts() when using the loop more than once? [duplicate]
  83. When to use wp_reset_postdata();
  84. Is including the loop necessary for page.php? [duplicate]
  85. the_title(); works in a page template, outside the loop. Why?
  86. wordpress loop for specific category
  87. Add 20yrs to post date, and then query
  88. Why do themes have `while( have_posts() )` in templates like single.php?
  89. When and Why is is_singular(‘my_cpt’) true while in_the_loop() is false?
  90. Three different post types on homepage
  91. What is best way passing variables to theme templates and using them different places like widgets?
  92. Apply styling only to first page sticky posts
  93. WordPress category & taxonomy loop with pagination
  94. Add Incrementing ID to each paragraph in the_content
  95. Changing behavior of the loop twice in one page
  96. How do I prevent one of two multiple loops from repeating on a second page?
  97. Insert/sticky specific post into Loop at specific location
  98. Style every four posts differently [duplicate]
  99. Return vs Echo Shortcode
  100. wp_list_categories: get latest featured_image of category
Categories loop Tags loop, post-meta, user-meta
Conditional Redirect
Don’t load the theme for a page FROM a plugin EDITED

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress