There may not be a need for a custom query, but I’d recommend it rather than hitting the database:
– once for the WP_Query
– once more for each post entry
And then manually working out the math in PHP. Do it all at once with one MySQL statement.
select sum(PM.meta_value) from wp_postmeta PM
join wp_posts P on P.ID = PM.post_id
where P.post_type="page"
and PM.meta_key='test'
With your code above, replace the $r=...
line with this:
$r = $wpdb->get_results( $wpdb->prepare( "
SELECT SUM(pm.meta_value) FROM {$wpdb->postmeta} pm
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
WHERE pm.meta_key = '%s'
AND p.post_status="%s"
AND p.post_type="%s"
", $key, $status, $type ) );
$r[0]
should be your total. 🙂
Related Posts:
- Lack of composite indexes for meta tables
- Safe to delete blank postmeta?
- How to get all term meta for a taxonomy – getting term_meta for taxonomy
- delete unused postmeta
- Triple meta_key on custom SELECT query
- Job of meta_key meta_value fields in database tables
- order by multiple meta_keys?
- WP_POSTMETA – What do these values mean inside the data structure?
- MySQL Query To Select Post By Postmeta
- How can I convert postmeta from unserialized to serialized?
- why after saving meta value it’s saving all the values the one that i clicked?
- Is it safe to add a new field to meta_value field?
- How to delete duplicate records in wp_postmeta database table?
- How can I use ‘orderby’ => ‘meta_value_num’ to order by the numerical value even if the value starts with a word?
- Display current ranking of post as a number in post title
- I can not display meta value in extras.php and template-tags.php
- Count Post and Page Views based on meta_value Using Shortcode in Dashboard Widget
- Negative meta_query if storing multiple post_meta values with shared meta_key
- if get_post_meta function returns empty – Do Not Display HTML
- How to get the total of two meta values from different meta keys?
- How to save a meta_value as a numeric value after I retrieve it via update_post_meta?
- How to update/add child posts meta whenever the parent post meta is updated?
- why is my postmeta table is so heavy
- Display multiple meta_key/meta_values by single SQL query
- Multiple meta key and value search in the query
- How to sort by meta value num, but ignore zero value?
- How do i get value from wp_postmeta?
- Using mysql queries to delete custom post types based on meta_value
- Export posts with postmeta without ID?
- How to only display posts whose meta_value field is not empty?
- Check if Post Title exists, Insert post if doesn’t, Add Incremental # to Meta if does
- Meta compare with date (stored as string) not working
- How to update/insert custom field(post meta) data with wordpress REST API?
- Front-end update_post_meta snippet displays white screen?
- How to get meta value in wp_attachment_metadata
- Clean up output added via wp_head()
- WordPress Admin Panel search posts with custom post meta values along with title
- Move value of one custom field to another
- Get post from meta_key and meta_value
- get_post_meta returns bool(false)
- How to get custom post type to display post meta on archive pages?
- mySQL query. ORDER BY meta_key
- WP_POSTMETA changes site crash
- meta_post_meta return value 1
- get_post_meta remains empty while looping all menu items and using the ID
- how do I set a schedule event to modify all posts’s meta value weekly or monthly?
- How to stop wp_postmeta from being called on archive and search pages?
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- How to wrap meta values seperated by comma in ? [closed]
- WordPress front-end media (image) upload ERROR!
- Run a check for multiple meta key values
- running function during post save and adding variable to post meta
- How to VAR_DUMP a $variable during checkout process (Is my product meta callable?)
- update_post_meta saves nothing in database when run in publish_post
- Can’t access post meta on new post creation (cpt)
- Custom post meta values reset by autosave [duplicate]
- Compare meta_query with a Regular Expression and do a less-than operation on it
- Is it possible to retrieve a post and its metadata at the same time?
- cleaning up safely wordpress wp_postmeta table
- Post meta as array looks like string instead of array
- Why does get_transient() always return string even if integer set?
- How to test the outcome of a wpdb query?
- Update post meta custom field using block editor
- Print specific values stored in a post meta array
- Is there a way to disable post meta caching for development?
- update_post_meta not working?
- Change wordpress meta tag description using WP functions
- Cant create or update meta fields using WordPress REST API
- Filter posts by meta key
- delete duplicates wp_postmeta
- Post Thumbnail missing when using webp format while sharing
- How do I echo all users’ display_name and their meta_value who have a certain meta_key?
- Bulk Update Post Meta Values from Different Post via Rest Api
- update_post_meta() is not saving the value
- Only show meta on one post type on search results page
- How to register post meta with multi level arrays?
- If I disable screen options, does WP still try to update post meta?
- Can we have duplicate key pair values in post meta data?
- I created a Custom Meta Box but it is not displaying the value on my post page
- Meta data being pulled from wp-login.php
- Get post content before rendering
- wp_insert_post inside save_post adds wrong metadata to inserted post
- Rename image filename using ‘media_handle_upload’
- Update Post metafield of specific categories
- Using Self Hosted Video URL With Custom Fields
- Multiple postmeta values to the same post_id/meta_key combination?
- get_post_meta not work in php foreach
- Why are my custom metaboxes not updating the post meta?
- Problem serializing single quote and double quote into post meta
- Storing post_meta fields in array
- Performace on 1 million plus meta fields vs 1 field with 1 million multi array
- Fetching array of postmeta with $wpdb and in_array conditional
- Piklist File Upload
- how to echo/display the custom field value in specific custom field name?
- update_post_meta() not working in bulk option
- How to update the ‘modified_time’ of a post ONLY when content is changed?
- Custom Field: Display only if a specific key is selected outside the loop
- Multiple If else statements on Search Result Page
- get_children filter with postmeta
- get_post_meta() returns nothing in save_post, publish_post, wp_after_insert_post