rcp_get_memberships()

Note: This is part of the developer docs and is considered custom code.
Unfortunately, we cannot provide support for custom code at this time as we do not have the additional resources that would be necessary to provide support for custom code. 

If you need assistance with this, please reach out to our list of consultants for further assistance:
https://codeable.io/developers/restrict-content-pro/

Query for memberships.

Parameters:

  • $args (array) - Array of arguments, all of which are optional:
    • $id (int) - A membership ID to only return that membership. Default empty.
    • $id__in (array) - An array of membership IDs. Default empty.
    • $id__not_in (array) - An array of membership IDs to exclude from the results. Default empty.
    • $customer_id (int) - A customer ID to only return this customer's memberships. Default empty.
    • $customer_id__in (array) - An array of customer IDs to return memberships belonging to these customers. Default empty.
    • $customer_id__not_in (array) - An array of customer IDs to exclude their memberships. Default empty.
    • $object_id (int) - ID of a membership level to only return memberships of this level. Default empty.
    • $object_id__in (array) - An array of membership level IDs to only return memberships of these levels. Default empty.
    • $object_id__not_in (array) - An array of membership level IDs to exclude memberships of these levels. Default empty.
    • $object_type (string) - An object type to only return memberships of this type. Default empty.
    • $object_type__in (array) - An array of object types. Default empty.
    • $object_type__not_in (array) - An array of object types to exclude. Default empty.
    • $currency (string) - A currency code to only return memberships using this currency. Default empty.
    • $created_date_query (array) - Date query clauses to limit memberships by. See WP_Date_Query for supported formats. Default empty.
    • $trial_end_date_query (array) - Date query clauses to limit memberships by. See WP_Date_Query for supported formats. Default empty.
    • $cancellation_date_query (array) - Date query clauses to limit memberships by. See WP_Date_Query for supported formats. Default empty.
    • $expiration_date_query (array) - Date query clauses to limit memberships by. See WP_Date_Query for supported formats. Default empty.
    • $auto_renew (int) - Filter by auto renew status (1 for recurring, 0 for not recurring). Default null.
    • $status (string) - A status slug, to only return memberships with this status. Supports: pending, active, cancelled, expired. Default empty.
    • $status__in (array) - Array of status slugs to include. Default empty.
    • $status__not_in (array) - Array of status slugs to exclude. Default empty.
    • $gateway_customer_id (string) - A gateway customer ID. Default empty.
    • $gateway_subscription_id (string) - A gateway subscription ID. Default empty.
    • $gateway (string) - A gateway slug. Default empty.
    • $subscription_key (string) - A subscription key. Default empty.
    • $upgraded_from (int) - ID of a membership. This will return only memberships that were upgraded from this one. Default empty.
    • $disabled (int|null) - Whether to show all memberships (null), only enabled memberships (0), or only disabled memberships (1). Default 0.
    • $meta_query (array) - An array of meta query arguments arrays (an array of arrays).
    • $count (bool) - Whether to return a membership count (true) or an array of membership objects (false). Default false.
    • $fields (string) - Item fields to return. Accepts any known column name, or empty to return an array of complete membership objects. Default empty.
    • $number (int) - Limit the number of results. Default 20.
    • $offset (int) - Number of memberships to offset. Default 20.
    • $no_found_rows (bool) Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
    • $orderby (string) - Accepts `id`, `object_id`, `object_type`, `currency`, `initial_amount`, `recurring_amount`, `created_date`, `trial_end_date`, `cancellation_date`, `expiration_date`, `times_billed`, `maximum_renewals`, `status`, `gateway_customer_id`, `gateway_subscription_id`, and `subscription_key`. Also accepts false, an empty array, or `none` to disable the `ORDER BY` clause. Default `id`.
    • $order (string) - How to order results. Accepts `ASC` and `DESC`. Default `DESC`.
    • $search (string) - Search term(s) to retrieve matching memberships for. This searches most of the above membership columns. Default empty.
    • $update_cache (bool) - Whether to prime the cache for found memberships. Default false.

Return values:

Examples:

Retrieve active memberships:

$memberships = rcp_get_memberships( array(
    'status' => 'active'
) );

Retrieve expired memberships that are on membership level #3:

$memberships = rcp_get_memberships( array(
    'object_id' => 3,
    'status' => 'expired'
) );

Retrieve 5 memberships that use the Stripe gateway:

$memberships = rcp_get_memberships( array(
    'number' => 5,
    'gateway' => 'stripe'
) );

Retrieve all memberships expiring between December 1st and December 31st and expire them now:

$memberships = rcp_get_memberships( array(
    'expiration_date_query' => array(
        'after' => date( '2018-12-01 00:00:00', current_time( 'timestamp' ) ),
        'before' => date( '2018-12-31 23:59:59', current_time( 'timestamp' ) ),
    ),
    'number' => 9999
) );

if ( ! empty( $memberships ) ) {
	foreach ( $memberships as $membership ) {
		$membership->expire();
	}
}

Retrieve all active memberships that have auto-renew enabled and cancel them:

$memberships = rcp_get_memberships( array(
    'status' => 'active',
    'auto_renew' => 1,
    'number' => 9999
) );

if ( ! empty( $memberships ) ) {
	foreach ( $memberships as $membership ) {
		if ( $membership->can_cancel() ) {
			$membership->cancel_payment_profile();
		}
	}
}

Retrieve all active memberships with the meta key "company" and value "Sandhills" :

$memberships = rcp_get_memberships( array(
    'status' => 'active',
    'number' => 9999,
    'meta_query' => array(
        array(
            'key' => 'company',
            'value' => 'Sandhills',
        )
    )
) );
Powered by Zendesk