Get All MailChimp Campaigns With MailChimp API v3 in WordPress

This is how to get a list of all campaigns in a MailChimp account using PHP. This can be used in WordPress.

This first function is a generic function to make any type of request to the MailChimp API version 3.

This first function is the same that I use in another MailChimp tutorial (Create and Send a MailChimp Campaign), so if you’re already using that code, you don’t have to add this first function again.

You must replace YOUR_API_KEY with your own MailChimp API key on line 15.

/**
 * Make a request to MailChimp API v3
 *
 * @param string $endpoint The MailChimp API endpoint for this request WITHOUT starting slash
 * @param string $type Type of request, whether GET, POST, PATCH, PUT, or DELETE
 * @param array $body The request body parameters
 *
 * @return $response object The response object
 * 
 */
function isa_mailchimp_api_request( $endpoint, $type = 'POST', $body = '' ) {
 
    // Configure --------------------------------------
 
    $api_key = 'YOUR_API_KEY';
 
    // STOP Configuring -------------------------------
 
    $core_api_endpoint = 'https://<dc>.api.mailchimp.com/3.0/';
    list(, $datacenter) = explode( '-', $api_key );
    $core_api_endpoint = str_replace( '<dc>', $datacenter, $core_api_endpoint );
 
    $url = $core_api_endpoint . $endpoint;
 
    $request_args = array(
        'method'      => $type,
        'timeout'     => 20,
        'headers'     => array(
            'Content-Type' => 'application/json',
            'Authorization' => 'apikey ' . $api_key
        )
    );
 
    if ( $body ) {
        $request_args['body'] = json_encode( $body );
    }
 
    $request = wp_remote_post( $url, $request_args );
    $response = is_wp_error( $request ) ? false : json_decode( wp_remote_retrieve_body( $request ) );
 
    return $response;
}

With the function above in place, now you can get a list of all the MailChimp campaigns. This is a simple one liner:

// Get all campaigns in a MailChimp account, with all data for each campaign.

$campaigns = isa_mailchimp_api_request( "campaigns", 'GET' );

The line above will retrieve all of the data for each campaign, which may be an overload of data, most of which may not be necessary for you. You can choose to get only the fields that you need. For example, the following line will only get the campaign ID, Title, and Status for each campaign.

// Get all campaigns in a MailChimp account, with only ID, Title, and Status for each campaign.

$campaigns = isa_mailchimp_api_request( "campaigns?fields=campaigns.id,campaigns.status,campaigns.settings.title", 'GET' );

See more:

We've 2 Responses

  1. January 9th, 2018 at 5:10 am

    Hi Isabel,

    Great code! It’s helping me a lot with a plugin I’m writing, but I have a question for you:

    Do you know how to get the campaigns ordered by “send_time”?
    Or how to get only the “scheduled” campaigns?

    At the moment I’m listing all the campaigns, but the order seems a bit random (probably is ordering by some field by default, but I don’t know which field)

    This is the code:

    $campaigns = isa_mailchimp_api_request( "campaigns?fields=campaigns.id,campaigns.settings.subject_line,campaigns.send_time,campaigns.status", 'GET' );
    

    If you can help me at this point, I’ll be very thankful.

    Thanks! And happy new year 🙂

    Daniel Mayans

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]