Search for keywords, like "PayPal", "Recurring Donations", and more.

Documentation / Developer Docs / How to Query Donor Information

How to Query Donor Information

Often times you may want to display donor information on your website from GiveWP. The following will demonstrate how to do this using GiveWP’s core classes. Keep in mind, not all donors want their information displayed publicly.

Note: The code snippets in this documentation display donors regardless of “anonymous” status of individual donations. Use these snippets with caution in production environments.

Getting Donors from Your GiveWP Database

The key to getting donors from your database is using the Give_Donors_Query class.

Get the latest 3 GiveWP Donors

<?php
/**
	 *  QUERY 1: Basic Example
	 *
	 *  This query outputs 20 donors
	 *  with the name, number of donations, and total donated amount
	 */

	// Query 1 Argument


	$args = array(
		'number' => 20,
	);
	$donor_query = new Give_Donors_Query( $args );
	$donor_query = $donor_query->get_donors();
	//print_r($donor_query);

	if ( $donor_query  ) {
		foreach ( $donor_query  as $donor ) {
			?>
			<!-- If you DO have donors that fit this query -->
			<p class="donor-name">
				Donor name: <?php echo $donor->name; ?>
			</p>
			<p class="num_donations">
				Number of donations: <?php echo $donor->purchase_count; ?>
			</p>
			<p class="amount_spent">
				Total donation amount: <?php $amount = give_currency_filter(give_format_amount($donor->purchase_value)); echo $amount; ?>
			</p>
			
			<?php
		}
	} else {
		?> 
		<!-- If you don't have donors that fit this query -->
			<h2>No donors to show!</h2>
			<?php
	}
?>

Understanding all the data you can Query for Donors

Often the easiest way to query data is to see a dump of the data to be able to customize the query. In the above snippet you pulled the name for each donor with $donor->name object/key.

The following snippet will similarly query the GiveWP Donor Object, but instead of returning 20 Donors, it will return one donor, and all of the various attributes of that object.

Get all data to use in Donor Queries

<?php

	/**
	 *
	 *  This code outputs all the donor metadata for your reference
	 * 
	 */


	$args = array(
		'number' => 1,
	);
	$donor_query = new Give_Donors_Query( $args );
	$donor_query = $donor_query->get_donors();
	//print_r($donor_query);

	if ( $donor_query  ) {
		//foreach ( $donor_query  as $donor ) {
			?>
			<div style="background: #555; color: white; padding: 2rem;">
				<h3>Donor Data</h3>
				<p>
					The following outputs all the "donor" fields for
					you to reference in building out your donor query
				</p>
				<pre>
					<?php var_dump( $donor_query ); ?>
				</pre>
			</div>
			<?php
		//}
	} 

?>

Conclusion

That gives you a starting point to pull any data you’d like for Donors from your GiveWP database.

Last updated 2 years ago

Start Fundraising Better Today!

Get GiveWP Today
GiveWP Plans

Give Fundraising Newsletter

The Give Fundraising Newsletter will help you navigate the world of online fundraising like a pro. Each week we send out fundraising advice, Give LIVE announcements, and exclusive offers to our newsletter subscribers.