functions - Update user meta of author when post content is viewed

admin2025-01-08  8

I have a value that I'm trying to store as user meta for authors on my site. I need it to update itself every time the content of a post is viewed for that author. Here's as far as I've gotten but it isn't storing the value. I use this in functions.php.

function user_score() {
    global $bp;
    $author_id = $bp->displayed_user->id; // do stuff to get user ID

    $author_posts = get_posts( array(
      'author' => $author_id,
      'posts_per_page' => -1
    ) );

    $counter = 0;
    foreach ( $author_posts as $post )
    {
      $score = get_post_meta( $post->ID, 'ratings_score', true );
      $counter += $score;
    }
    update_user_meta( $author_id, 'user_score', $counter );
    if ($echo)
      echo $counter;
    return $counter;
}
add_filter('the_content','update_user_score');
function update_user_score($content){
  global $post;
  user_score();
  return $content;
}

I have a value that I'm trying to store as user meta for authors on my site. I need it to update itself every time the content of a post is viewed for that author. Here's as far as I've gotten but it isn't storing the value. I use this in functions.php.

function user_score() {
    global $bp;
    $author_id = $bp->displayed_user->id; // do stuff to get user ID

    $author_posts = get_posts( array(
      'author' => $author_id,
      'posts_per_page' => -1
    ) );

    $counter = 0;
    foreach ( $author_posts as $post )
    {
      $score = get_post_meta( $post->ID, 'ratings_score', true );
      $counter += $score;
    }
    update_user_meta( $author_id, 'user_score', $counter );
    if ($echo)
      echo $counter;
    return $counter;
}
add_filter('the_content','update_user_score');
function update_user_score($content){
  global $post;
  user_score();
  return $content;
}
Share Improve this question edited May 21, 2013 at 21:20 s_ha_dum 65.5k13 gold badges84 silver badges174 bronze badges asked Jun 19, 2012 at 8:56 Pollux KhafraPollux Khafra 8884 gold badges25 silver badges50 bronze badges 2
  • Do you have debugging information? – s_ha_dum Commented May 21, 2013 at 21:20
  • 3 For the record, this is a horrible thing to do on every page load. If your site has very low traffic it's fine but as soon as you scale you will choke the database with read/write calls. It's better to parse access logs on a set interval so you can do them in bulk and keep your site up. This will also fail if your site does any caching of page content. – jgraup Commented Nov 6, 2016 at 3:53
Add a comment  | 

2 Answers 2

Reset to default 1

I would use the action wp_footer. You could do something like below. You do not need to loop through all posts whenever a post is showing. Just add the current post score to user for the current post.

add_action('wp_footer', 'wpse55725_update_user_score');

function wpse55725_update_user_score(){
    if( !is_singular('post') )    
        return;    // only run for single post

    global $post;
    $current_score = get_user_meta( $post->post_author, 'user_score', true ); // get current score of the user
    $score_points = get_post_meta( $post->ID, 'ratings_score', true ); // score for the post
    $score = $current_score + $score_points; // add the score to users current score

    update_user_meta( $post->post_author, 'user_score', $score); // save it

}

Code not tested.

untested but should work

   add_filter('the_content','update_user_score');
    function update_user_score($content){
        global $post;
        $counter = 0;
        $score = get_post_meta( $post->ID, 'ratings_score', true );
        $counter += $score;
        update_user_meta( $post->post_author, 'user_score', $counter);
        return $content;
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270941a1469.html

最新回复(0)