Wordpress meta query
Use below code will work as per your scenario.
add_filter( 'pre_get_posts', 'filterReports' );function filterReports( $query ) { if( is_post_type_archive( 'reports' ) && $_GET['top'] ) { $reports_meta_query = $query->get('meta_query'); //Add our meta query to the original meta queries $reports_meta_query[] = array( 'key'=>'count', 'value'=> 4, 'compare'=>'>=', ); $query->set('meta_query',$reports_meta_query); // somehow construct a query that checks if sum / count >= 4 }}
Pretty sure the query you are looking for is something like this:
SELECT sum.post_id, sum.meta_value, count.meta_value, (sum.meta_value / count.meta_value) as result FROM wp_postmeta sum LEFT JOIN wp_postmeta count USING(post_id) WHERE sum.meta_key = 'sum' AND count.meta_key = 'count' HAVING result >= 4
You are basically joining twice the same table based on the post_id, so you can then query by the meta_key of both sum and count, then you look for the result of your math in a Having
clause to check if the result would be bigger than 4 as requested.
Hope with this you can get what you were looking for.
Cheers