Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

How to add together (get the sum) a field from all author’s posts

First you have to declare the global $wpdb before using it. Also you need to include the table prefix when calling the table names.

global $wpdb;
$posts_table = $wpdb->prefix . "posts";
$posts_meta_table = $wpdb->prefix . "postmeta";

Next get an array of post ids by the author needed( make sure you have the id of desired author, assign to $author_id).

$authors_posts = $wpdb->get_col($wpdb->prepare("SELECT * FROM ".$posts_table." WHERE post_author = %d", $author_id) );

Make a comma delimited version of the post id array for SQL

$comma_delimited = implode(',', array_map('intval', $author_posts));

Next use the id array to make a sum of ‘epicredvote’ from the post meta table. Note the # of votes is stored in the meta_value column

$allscore = $wpdb->get_var( $wpdb->prepare(
    "SELECT SUM(meta_value) 
    FROM " .$posts_meta_table . "
    WHERE (meta_key = 'epicredvote' 
           AND post_id IN %s)"
   , $comma_delimited));

Related Posts:

  1. How to update custom fields using the wp_insert_post() function?
  2. Get meta_id along with meta_key and meta_value
  3. Display user’s total comment count outside The Loop
  4. How can merge two arrays values in one array and save in database
  5. Check if user already visited this post today
  6. Display all meta for a post?
  7. How to display multiple Post meta_key/meta_values by SQL query
  8. the_post_thumbnail() based on the Post ID
  9. How to use multiple query with same meta key
  10. How to update single value in multi dimensional Post Meta?
  11. Why is my get_post_meta always empty
  12. Specific Loop For 2 Within Each
  13. Compare two meta key values against each other inside the get_posts array?
  14. How can update custom meta for all posts
  15. How to change post count in wordpress loop?
  16. Use ajax to update_post_meta
  17. Count number of published posts by type
  18. how to increase custom post value by one most efficiently?
  19. Batch update a post meta field value of each post in Post Type
  20. How to Display Image Meta underneath EVERY image in EVERY post
  21. Change description on specific WooCommerce product status change
  22. Use WP_Query in shortcode
  23. Adding country tags automatically
  24. Get Current User Id Inside a Loop Returns 0 For a Shortcode
  25. Random order of WP_Query results with highest meta value
  26. The text box have space character
  27. how to remove metadata from the posts of my blog?
  28. How can I create a list of page titles from custom meta values?
  29. Trying to remove post thumbnail with plugin
  30. Randomly Assign an Image’s Alt Text Based on Data From Post
  31. Advanced Meta Query for Large Calendar Website (12k+ posts) (175k+ wp_postmeta rows)
  32. User’s total comment count excluding comments on their own posts
  33. Sort custom meta column by other meta value
  34. Store multiple custom field as post meta per post(css, js, html, 2 link) [closed]
  35. How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
  36. Creating user status mode in WordPress
  37. Need help with Google drive API [closed]
  38. Archive post by meta value + 24hours
  39. WordPress update_post_meta updating with empty meta_value field [closed]
  40. Is it possible to retrieve all posts with a certain value for metadata?
  41. Get post id within comments loop
  42. Non-Closing PHP Query in WordPress Loop
  43. Querying Database with wpdb
  44. update_post_meta only updating on last loop of foreach
  45. How can i count the post added in relationship field, theme display
  46. how to get serialized post meta
  47. How to use two meta_compare in an array?
  48. In WordPress, how do I get the number of posts next to single_cat_title(”); in the category.php file?
  49. Remove empty terms from array, sort alphabetically, update back to repeating field
  50. WordPress loop by meta key that is an array? and how loop multiple arrays
  51. How can I count ACF sub_field with a certain value
  52. WordPress yoast seo plugin – parse snippet var in meta description
  53. How to store multiple custom meta box
  54. How to add post meta in while loop?
  55. Add category to post meta
  56. Using rules in Posts
  57. Update post meta – Custom field does not match meta-key
  58. Order a WP_Query by meta value where the value is an array
  59. Save the value of a wp_dropdown_pages
  60. Add a custom class to the body tag using custom fields
  61. Define category ID using get_post_meta
  62. Unable to Call More than One Meta Box Output
  63. Hide a div when a custom field is empty
  64. Taxonomy term count
  65. Update value of a associative array with update_post_meta
  66. Front end file upload returning wrong attachment url
  67. Post meta select input, if statement
  68. WordPress stripping out custom field tags
  69. How to check post meta isset or not empty during publish/save post?
  70. Counting number of posts in multiple (sub)categories using shortcode
  71. get value from get_post_meta then reuse it in another get_post_meta
  72. Order category posts by last word of custom field
  73. Add a “custom field” to a category that can be retrieved when viewing the category page with get_post_meta
  74. Loop for recently (updated_post_meta) posts?
  75. How to add and subtract user meta values after post meta update
  76. How to update custom fields when post is published?
  77. Update Images after edit via php
  78. Like and Dislike Buttons on Post with Counter – Only allow one click per post per user session
  79. How can I show how much water got saved on this order (making use of the number of items bought) on a thank-you-page?
  80. No more get_option(‘date_time’) in WordPress 5.5?
  81. Delete post meta by serialized meta value
  82. Efficient way of querying for a “fallback” post?
  83. WordPress AJAX load post metadata in modal
  84. For each 3 posts, show a different post type
  85. get_post_meta with variable
  86. WP_Query count of different meta key values [duplicate]
  87. Post from front end form to post_meta
  88. divide custom field values in div every two values
  89. Order by in foreach
  90. retrieve wordpress post-meta using php not wordpress functions
  91. Postmeta not saving
  92. If custom field doesn’t exist, or exists and it’s true show title?
  93. get_post_meta is always empty when I use wp_mail
  94. How to get the count for each taxonomy term
  95. get_post_meta no longer working
  96. Metadata on the WP_Post object
  97. How to display the date under the post title?
  98. How do I get a function to work in single.php
  99. Count the number of matching post names in foreach loop
  100. How to modify default tags in wp_head()
Categories PHP Tags count, php, post-meta
Use cookies in pagination
Sending Parameter Failed

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress