You can still do the AS
with $wpdb
, I find it still makes stuff look cleaner. You can use the $wpdb->prepare()
method for sanitation. Are you looking for something like this?
function combined_downloads($post_id)
{
global $wpdb;
return $wpdb->get_var($wpdb->prepare(
"SELECT SUM( meta_value ) FROM $wpdb->postmeta AS m
LEFT JOIN $wpdb->posts AS p
ON m.post_id = p.ID
INNER JOIN {$wpdb->prefix}p2p AS r
ON m.post_id = r.p2p_to
WHERE m.meta_key = '_download_count'
AND p.post_type="dlm_download"
AND p.post_status="publish"
AND r.p2p_from = %d
", $post_id ));
Then you can call the function from the loop:
$count = combined_downloads($post->ID);
$count
will have the result of the query and you can echo it where you like. If you need the function outside the loop, you can do this:
$count = combined_downloads($wp_query->post->ID);