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>";
}