Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
This is really a PHP question and without showing some code unlikely to get any answers. –  Wyck Jul 2 '12 at 22:54
    
I didn't see any point in presenting any code since anyone who knows the answer already knows how to get user meta. –  Pollux Khafra Jul 2 '12 at 23:08
1  
Well, you saw that I made an erroneous answer (deleted), it was a misreading on my part, but nonetheless a good Question does have all details you can provide: relevant code, research efforts, etc... –  brasofilo Jul 2 '12 at 23:52
1  
Yeap my mistake. –  Pollux Khafra Jul 2 '12 at 23:54
    
No problem, for me is always a good reminder to fully understand the question before answering. And as a questioner one should try to consider that less experienced folks may find useful info in the Q as well as in the A. –  brasofilo Jul 3 '12 at 0:20
add comment

closed as off topic by Wyck, Chris_O, Brady Oct 2 '12 at 16:55

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 1 down vote accepted

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:

if( is_admin() )
{
    add_action( 'save_post', 'wpse_57217_check_customfield_variation', 11, 2 );
}

function wpse_57217_check_customfield_variation( $post_id, $post )
{
    if ( 
        ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        or ! current_user_can( 'edit_post', $post_id )
        or wp_is_post_revision( $post )
    )
    { // Noting to do.
        return;
    }

    $actual = get_post_meta($post_id, 'score', true);
    $last = get_post_meta($post_id, '_score_last', true);
    $last = ( '' != $last ) ? $last : '0';
    if ( '' != $actual ) 
    {
        if ( absint($actual) > absint($last) )
        {
            update_post_meta( $post_id, '_score_variation', 'up' );
        }
        elseif ( absint($actual) == absint($last) )
        {
            update_post_meta( $post_id, '_score_variation', 'stable' );
        }
        else
        {
            update_post_meta( $post_id, '_score_variation', 'down' );
        }
        update_post_meta( $post_id, '_score_last', $actual );
    } 
}

And for reference only, this inside the loop:

echo 'actual score: ' . get_post_meta($post->ID, 'score', true);
echo '<br> last score (value already updated): ' . get_post_meta($post->ID, '_score_last', true);
echo '<br> score variation: ' . get_post_meta($post->ID, '_score_variation', true);
share|improve this answer
    
This is a wicked work up only the score is user meta not post meta. Reading over my question I should have made it a little more clear. Im going to edit. –  Pollux Khafra Jul 2 '12 at 23:40
add comment

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.

share|improve this answer
    
Can you hold my hand and show how to do that? –  Pollux Khafra Jul 3 '12 at 0:02
    
I'm un-deleting my mistaken answer with full sample code on how to do that with Custom Fields. Consider your hand already hold :) –  brasofilo Jul 3 '12 at 0:24
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.