I store a custom user meta value for my users that is stored as a number like a score. I need a way of telling whether it has decreased or increased from its last value. So when I display it I can show an arrow to show which it’s trending. Any ideas?
Here is how I get the score and update the user meta.
add_filter('the_content','update_user_score');
function update_user_score($content){
global $post;
$author_id = $post->post_author;
$author_posts = get_posts( array(
'author' => $author_id,
'posts_per_page' => -1
) );
$counter = 0;
foreach ( $author_posts as $author_post )
{
$score = get_post_meta( $author_post->ID, 'ratings_score', true );
$counter += $score;
}
update_user_meta( $author_post->post_author, 'score', $counter);
return $content;
}
I would solve this creating two extra Custom Fields:
_score_last
_score_variation
The first underscore makes the CF invisible in the Admin area.
Drop the following code in your theme’s
functions.php
:And for reference only, this inside the loop:
Simple – you need 2 separate meta values, one for current score, one for either last score or score trend. With a single value stored, WordPress has no way of knowing anything but that value – no way to tell if it’s the first time it’s been set or the millionth time.