Last active
October 23, 2021 18:34
-
-
Save helgatheviking/b08b1839baee95c506bd909f2f30e6ef to your computer and use it in GitHub Desktop.
Shortcode to output unique donor count for specific form (needs caching)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Give count unique donors | |
* | |
* @param array $atts [ | |
* 'form_id' => int The form ID to count donors for | |
* ] | |
*/ | |
function kia_give_donor_count( $atts ) { | |
global $wpdb; | |
$atts = shortcode_atts( array( | |
'form_id' => 0 | |
), $atts, 'give_donor_count' ); | |
if ( empty ( $atts[ 'form_id' ] ) ) { | |
return; | |
} | |
$sql = $wpdb->prepare( " | |
SELECT | |
COUNT( DISTINCT (rel2.meta_value ) ) as donor_count | |
FROM {$wpdb->posts} AS posts | |
LEFT JOIN {$wpdb->prefix}give_donationmeta AS rel ON | |
posts.ID = rel.donation_id | |
LEFT JOIN {$wpdb->prefix}give_donationmeta AS rel2 ON | |
posts.ID = rel2.donation_id | |
WHERE | |
posts.post_type = 'give_payment' AND | |
posts.post_status = 'publish' AND | |
rel.meta_key = '_give_payment_form_id' AND | |
rel.meta_value = %d AND | |
rel2.meta_key = '_give_payment_donor_email' | |
", | |
intval( $atts[ 'form_id' ] ) | |
); | |
$result = $wpdb->get_var( $sql ); | |
return '<p class="donor_count">' . sprintf( esc_html__( 'Total donors: %d', 'your-textdomain' ), $result ) . '</p>'; | |
} | |
add_shortcode( 'give_donor_count', 'kia_give_donor_count' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment