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. How do I get the attributes of a short code from a post?
  22. WordPress category & taxonomy loop with pagination
  23. Changing behavior of the loop twice in one page
  24. Show scheduled posts in archive page
  25. post__in – Placing content from a foreach loop inside of an array
  26. loop query exclude meta_key with meta_value
  27. “Blog pages show at most” in The Loop
  28. wp_insert_posts Fatal error: Maximum function nesting level of ‘100’ reached, aborting!
  29. Post Loop not Returning Permalink
  30. Displaying child page content of a certain parent
  31. Pagination adding extra posts only on page 2
  32. add_filter() doesn’t work in loop
  33. The Loop in Static Page
  34. Content/Excerpt length control for a specific loop?
  35. Why we use if with while loop?
  36. Loop posts only excluding first post
  37. Loop repeating design pattern
  38. Sort Posts By Category?
  39. Can’t access login screen, wp-login.php 404’s
  40. Create a loop that gets pages with their template
  41. Why is my loop not dynamically grabbing the correct Category and displaying all categorized posts?
  42. Query posts if meta key starts with
  43. 3 Posts in Loop, Show Stickies First
  44. Loop within a loop (Again) for template
  45. How to use current_post to open a new unordered list every five posts
  46. Why cant you place the Loop outside of the index.php?
  47. sticky post in custom loop
  48. Rating system and changing the loop
  49. Ajax Button to load more Posts into a timeline
  50. How to target thumbnails of the first post in the loop
  51. Showing sticky posts with get_posts()
  52. Pagination not working in category listing [duplicate]
  53. How to get the excerpt for is_home() outside of the loop isn’t working
  54. Category links including all posts
  55. Custom loop ordering not working
  56. First archive page with a few posts
  57. Hiding a row in the loop if empty
  58. Loop through ACF taxonomies and output associated posts
  59. Query posts only with actual text content (not including shortcode or images)
  60. Fetching posts from wordpress function in ajax
  61. Remember the Meta Value With Radio Buttons
  62. Saving return value from the_author_posts_link()
  63. how to run loop in function.php that sends email based on specific conditions?
  64. How to use this $tax_selection variable in this custom loop?
  65. Display all categories as plain text
  66. How to sort a loop after most viewed
  67. How to add and subtract user meta values after post meta update
  68. WordPress posts loop not displaying all posts on blog
  69. get_page_by_title not working inside fucntions.php
  70. Bootstrap tabs are not being clicked in WordPress loop
  71. Get gallery in loop through ajax
  72. Get a specific size from wp_get_attachment_image_src
  73. Pagination on a underscore custom theme
  74. Get top Page IDs from menu and cycle through their child pages on a scroller
  75. Style first 3 posts differently and use a 2nd loop to get rest of posts / offset and pagination broken
  76. Posts Per Page is Not Returning Correct Number
  77. Custom Looping of WordPress Posts
  78. how to place Post and page content side by side
  79. Add specific class to featured posts
  80. Get meta value when the page is a blog archive
  81. Echo Most Recent Sticky Post in Loop?
  82. ACF field not appearing correctly in loop
  83. Loop in taxonomy for terms and post
  84. WordPress Tags in class
  85. The Loop isn’t working
  86. Changing the loop w/o killing category links
  87. Custom loop page with post navigation
  88. Pages with a loop (index, archive) are loading the first image as post_thumbnail
  89. the loop – how to control whether wp or plug-in runs it
  90. stuck with template hierarchy
  91. How to create an identical second loop for attachments?
  92. How to show only specific tag in wordpress loop
  93. Different style for first two (sticky) posts
  94. WooCommerce – Checkout suddenly stops working [closed]
  95. pull 500 post of many from database [duplicate]
  96. Want to images load first then title in WordPress loop
  97. How to add a continuous number to HTML tag attribute value inside The Loop [closed]
  98. WordPress Not Sorting By Custom Field
  99. Landing page with login
  100. How can I create an entirely new, separate display of posts?
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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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