slicewp_get_affiliates()

Description

By default, the function returns an array of the latest registered affiliates, or affiliates matching the given arguments.

If the $count parameter (detailed below) is set to true, instead of the array with results, the function will return the results count.

Parameters

slicewp_get_affiliates( $args = array(), $count = false );
  • $args (array)(optional) Array of arguments to retrieve affiliates.
    • number (int) The number of affiliates to query for. Defaults to 20.
    • offset (int) The number of affiliates to offset the query by. Defaults to 0.
    • orderby (string) The affiliates column to order results by. Default to “id”.
    • order (string) The order in which to return the results. Accepts “ASC” and “DESC” values. Defaults to “DESC”.
    • status (string) The status of the affiliate. Defaults to empty string.
    • include (array) An array of affiliate IDs to specifically return. Defaults to empty array.
    • user_id (int|array) A user’s ID or an array of user IDs that correspond to the affiliate(s). Defaults to empty string.
  • $count (bool)(optional) Whether to return the total count of the results, instead of the actual results. Defaults to false.

Return

(array|int) Array of SliceWP_Affiliate objects or the results count if the $count parameter is set to true.

Examples

Get all affiliates

By default, the function will return the last 20 affiliates. This example shows how you can retrieve all affiliates.

$args = array(
    'number' => -1
);

$affiliates = slicewp_get_affiliates( $args );
// Returned value.

Array
(
    [0] => SliceWP_Affiliate Object
        (
            [id:protected] => 3
            [user_id:protected] => 12
            [date_created:protected] => 2021-08-26 10:48:22
            [date_modified:protected] => 2021-08-26 10:48:22
            [status:protected] => pending
            [payment_email:protected] => joanna_doe@joannadoe.com
            [website:protected] => https://joannadoe.com/
        )

    [1] => SliceWP_Affiliate Object
        (
            [id:protected] => 2
            [user_id:protected] => 11
            [date_created:protected] => 2021-08-23 13:30:59
            [date_modified:protected] => 2021-08-23 13:30:59
            [status:protected] => active
            [payment_email:protected] => jimmy_doe@jimmydoe.com
            [website:protected] => https://jimmydoe.com/
        )

    [2] => SliceWP_Affiliate Object
        (
            [id:protected] => 1
            [user_id:protected] => 10
            [date_created:protected] => 2021-08-23 11:19:27
            [date_modified:protected] => 2021-08-23 11:19:27
            [status:protected] => active
            [payment_email:protected] => john_doe@johnnydoe.com
            [website:protected] => https://johnnydoe.com/
        )
)

Because the SliceWP_Affiliate object’s properties are protected, to access them you can use the get( $property_name ) method.

$affiliates = slicewp_get_affiliates();

foreach ( $affiliates as $affiliate ) {

    $user_id = $affiliate->get( 'user_id' );
    $status  = $affiliate->get( 'status' );

}

Get the count of all affiliates

To retrieve the count for all your affiliates, you can query similarly to the previous example, but also set the $count parameter to true.

$args = array(
    'number' => -1
);

$affiliates = slicewp_get_affiliates( $args, true );
// Returned value.

3

Get all affiliates that have a certain status

By default, the function will return affiliates of all statuses. This example shows how you can return all affiliates of a certain status.

$args = array(
    'number' => -1,
    'status' => 'pending'
);

$affiliates = slicewp_get_affiliates( $args );
// Returned value.

Array
(
    [0] => SliceWP_Affiliate Object
        (
            [id:protected] => 3
            [user_id:protected] => 12
            [date_created:protected] => 2021-08-26 10:48:22
            [date_modified:protected] => 2021-08-26 10:48:22
            [status:protected] => pending
            [payment_email:protected] => joanna_doe@joannadoe.com
            [website:protected] => https://joannadoe.com/
        )
)

Get the count of affiliates with a certain status

$args = array(
    'number' => -1,
    'status' => 'pending'
);

$affiliates = slicewp_get_affiliates( $args, true );
// Returned value.

1

Get affiliates corresponding to certain users

In SliceWP affiliates are tied to users. You cannot have an affiliate without having a corresponding WP_User. This example shows you how to retrieve affiliates by their corresponding user_id.

$args = array(
    'user_id' => array( 10, 12 )
);

$affiliates = slicewp_get_affiliates( $args );
// Returned value.

Array
(
    [0] => SliceWP_Affiliate Object
        (
            [id:protected] => 3
            [user_id:protected] => 12
            [date_created:protected] => 2021-08-26 10:48:22
            [date_modified:protected] => 2021-08-26 10:48:22
            [status:protected] => pending
            [payment_email:protected] => joanna_doe@joannadoe.com
            [website:protected] => https://joannadoe.com/
        )

    [1] => SliceWP_Affiliate Object
        (
            [id:protected] => 1
            [user_id:protected] => 10
            [date_created:protected] => 2021-08-23 11:19:27
            [date_modified:protected] => 2021-08-23 11:19:27
            [status:protected] => active
            [payment_email:protected] => john_doe@johnnydoe.com
            [website:protected] => https://johnnydoe.com/
        )
)

Get only certain affiliates by their ID

$args = array(
    'include' => array( 2, 3 )
);

$affiliates = slicewp_get_affiliates( $args );
// Returned value.

Array
(
    [0] => SliceWP_Affiliate Object
        (
            [id:protected] => 3
            [user_id:protected] => 12
            [date_created:protected] => 2021-08-26 10:48:22
            [date_modified:protected] => 2021-08-26 10:48:22
            [status:protected] => pending
            [payment_email:protected] => joanna_doe@joannadoe.com
            [website:protected] => https://joannadoe.com/
        )

    [1] => SliceWP_Affiliate Object
        (
            [id:protected] => 2
            [user_id:protected] => 11
            [date_created:protected] => 2021-08-23 13:30:59
            [date_modified:protected] => 2021-08-23 13:30:59
            [status:protected] => active
            [payment_email:protected] => jimmy_doe@jimmydoe.com
            [website:protected] => https://jimmydoe.com/
        )
)

Was this article helpful?

Do you have any feedback or suggestions to improve this page?

Can't find what you're looking for? We're here to help.

Contact us