RCP Memberships grouped by Membership Levels in Bricks

This Pro tutorial provides the steps to show Restrict Content Pro‘s membership level as headings and, within them, a list of customer names having active memberships under each level in Bricks.

Consider this sample data:

After implementing the tutorial:

Step 1

Let’s register a custom query type for Bricks called “RCP Membership Levels”.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php

/**
 * Get active membership levels and create arrays of options.
 *
 * @return array{names: array<string>, ids: array<int>} An associative array containing:
 *                                                      'names' => array of level names
 *                                                      'ids'   => array of level IDs
 */
function bl_get_membership_level_options(): array {
    $levels = rcp_get_membership_levels( 'active' );

    if ( empty( $levels ) ) {
        return [
            'names' => [],
            'ids'   => [],
        ];
    }

    $names = [];
    $ids   = [];

    foreach ( $levels as $level ) {
        $names[] = $level->name;
        $ids[]   = $level->id;
    }

    return ['names' => $names, 'ids' => $ids];
}

// Add new query type control called "RCP Membership Levels"
add_filter( 'bricks/setup/control_options', function ( array $control_options ): array {
    $control_options['queryTypes']['bl_rcp_membership_levels'] = esc_html__( 'RCP Membership Levels' );

    return $control_options;
} );

// Return an array of all active RCP membership levels.
add_filter( 'bricks/query/run', function( $results, $query_obj ) {
    if ( $query_obj->object_type !== 'bl_rcp_membership_levels' ) {
        return $results;
    }

    return bl_get_membership_level_options()['names'];
}, 10, 2 );

if ( ! function_exists( 'bl_get_loop_object_string' ) ) {
    // Function to return the value of the looping object string.
    function bl_get_loop_object_string(): string {
        // check if there is any active query looping (nested queries) and if yes, store the query ID of the most deep query
        $looping_query_id = BricksQuery::is_any_looping();

        // get_query_object_type gets the current query object type (post, term, user or a custom one)
        // if the query ID of the most deep query exists and the current query object type is bl_rcp_membership_levels..
        if ( $looping_query_id && BricksQuery::get_query_object_type( $looping_query_id ) === 'bl_rcp_membership_levels' ) {
            return BricksQuery::get_loop_object( $looping_query_id );
        }

        return '';
    }
}

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_get_loop_object_string',
  ];
} );

Step 2

Edit your Page/template with Bricks.

Add a Section and inside its Container, a Block.

Click the query icon for the Block and select “RCP Membership Levels” query type.

Add a h3 heading inside the Block.

Set its text to

{echo:bl_get_loop_object_string}

That will output the membership level name.

Next, we will get the membership level ID for each query loop iteration and output an unordered list of customer’s/customers’ display names for that membership level.

Add a Code element below the heading (at the same level as the heading) having:

<?php

$index = intval( BricksQuery::get_loop_index() );

if ( isset( bl_get_membership_level_options()['ids'][$index] ) ) {
	// get active memberships for the current membership level being looped through
	$memberships = rcp_get_memberships( [
		'object_id' => bl_get_membership_level_options()['ids'][$index], // membership level ID
		'status' => 'active'
	] );

	// loop through the memberships for the current level and output user display name(s)
	echo "<ul>";
	foreach ( $memberships as $membership ) {
		echo '<li>' . esc_html( get_userdata( $membership->get_customer()->get_user_id() )->display_name ) . '</li>';
	}
	echo "</ul>";
}

?>

Notes

References

https://restrictcontentpro.com/knowledgebase/rcp_membership/

https://restrictcontentpro.com/knowledgebase/rcp_customer/