Add a custom field value for all of an Author’s posts

I’m trying to add a custom field value for all of each author’s posts and display it on the author’s profile page.

I have a Like button function I created where when a user likes a post it adds +1 to post custom field _post_like_count so if a post has 10 likes the value in _post_like_count is 10.

Read More

What I’m looking to do is take all of each author’s posts add up the total likes they have for all their posts combined so if they have 5 posts that each have 10 likes each the total value should be 50.

$author_posts = get_posts( array('author' => $author->ID) ); // Author's ID
$total_like_count = 0; // needed to collect the total sum of likes
foreach ( $author_posts as $post ) {
    $likes = absint( get_post_meta( $post->ID, '_post_like_count', true ) );
$total_like_count += $likes;
}

This is the php I”m using which I thought should work and it does for some of the authors it appears but not for others, as I have some author’s that should have 1000+ likes and it displays 77, where if an author has just 1 post with 17 likes it will display that properly.

For a little about what I”m trying to do on the top line I’m getting the post author’s ID, then I run the foreach to get each post and store the meta field value in $likes variable and then calculate it up in the bottom line in the $total_like_count which should be the number of all the likes for the author’s posts combined.

Related posts

2 comments

  1. Your get_posts() function is only pulling the 10 most recent posts of that author (or whatever number you have set as the default posts per page). Add 'nopaging' => true to get all of the author’s posts.

    Also, if you’re doing this inside the loop, using $post in your foreach loop may be problematic. Try switching it to something like $author_post and then use:

    $likes = absint( get_post_meta( $author_post->ID, '_post_like_count', true ) );
    
  2. Your problem can be solved by setting posts_per_page to -1 but I’d be tempted to run a custom database query instead. The benefit of doing it this way is you’re only running one SQL query.

    By using get_posts you’re running a SQL query every time you call get_post_meta which is incredibly inefficient. You may not notice it if an author has 10 posts but once you get into the hundreds the impact will be apparent.

    Example:

    function wpse_get_author_like_count( $author_id ) {
    
        global $wpdb;
    
        $like_count = $wpdb->get_var( $wpdb->prepare( "
            SELECT SUM( mt.meta_value ) 
            FROM {$wpdb->postmeta} AS mt
            LEFT JOIN {$wpdb->posts} p ON p.ID = mt.post_id
            WHERE mt.meta_key = '_post_like_count'
            AND p.post_type = 'post',
            AND p.post_status = 'publish'
            AND p.post_author = %d
        ", $author_id ) );
    
        return (int) $like_count;
    
    }
    

    Then to use it you would write:

    $author_like_count = wpse_get_author_like_count( 10 ); // replacing this with the correct author ID of course.
    

    This was coded on the fly by the way so I can’t guarantee it’s totally accurate. I am curious to know the result.

Comments are closed.