Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shortcode to output unique donor count for specific form (needs caching)
<?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