Some site owners allow their affiliates to write blog posts. The following code snippet will show how to retrieve the current post author’s affiliate ID within AffiliateWP. If the post author is not an affiliate, it will return false.

<?php
/**
 * Get the post author's Affiliate ID (if they're an affiliate).
 *
 * @link https://andrew.dev/snippets/get-post-authors-affiliate-id/
 *
 * @return int|false The ID of the affiliate, otherwise false.
 */
function am_affwp_get_post_authors_affiliate_id() {

	global $post;

	// Bail if AffiliateWP is not active.
	if ( ! function_exists( 'affiliate_wp' ) ) {
		return false;
	}

	// Get the post author's ID for the current post.
	$post_author_id = (int) $post->post_author;

	// Get the post author's affiliate ID.
	$affiliate_id = (int) affwp_get_affiliate_id( $post_author_id );

	// Return the affiliate ID.
	if ( $affiliate_id ) {
		return $affiliate_id;
	}

	return false;

}

Leave a Reply

Your email address will not be published. Required fields are marked *