LLMS_Membership::get_associated_posts( string $post_type = null )

Retrieve a list of posts associated with the membership


Description Description

An associated post is:

  • A post, page, or custom post type which supports llms-membership-restrictions and has restrictions enabled to this membership
  • A course that exists in the memberships list of auto-enroll courses
  • A course that has at least one access plan with members-only availability linked to this membership

Top ↑

Parameters Parameters

$post_type

(string) (Optional) If supplied, returns only associations of this post type, otherwise returns an associative array of all associations.

Default value: null


Top ↑

Return Return

(array[]|int[]) An array of arrays of post IDs. The array keys are the post type and the array values are arrays of integers. If $post_type is supplied returns an array of associated post ids as integers.


Top ↑

Source Source

File: includes/models/model.llms.membership.php

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
public function get_associated_posts( $post_type = null ) {
 
    // If we're querying only posts, we can skip these associations entirely because courses don't support them.
    $post_types = 'course' !== $post_type ? get_post_types_by_support( 'llms-membership-restrictions' ) : array();
 
    // If we're looking at a single post type we only have to query associations for that post type.
    $post_types = $post_type ? array_intersect( $post_types, array( $post_type ) ) : $post_types;
 
    // Our return array.
    $posts = array();
 
    // Retrieve all posts that are restricted to a membership via a LifterLMS Membership Restriction setting.
    foreach ( $post_types as $type ) {
        $posts[ $type ] = $this->query_associated_posts( $type, '_llms_is_restricted', 'yes', '_llms_restricted_levels' );
    }
 
    // Include courses if courses were requested or if no specific post type was requested.
    if ( ! $post_type || 'course' === $post_type ) {
        $posts['course'] = $this->query_associated_courses();
    }
 
    /**
     * Filter the list of posts associated with the membership.
     *
     * @since 3.38.1
     *
     * @param array[]         $posts     An array of arrays of post IDs. The array keys are the post type and the array values are arrays of integers.
     * @param string|null     $post_type The requested post type if only a specific post type was requested, otherwise `null` to indicate all associated post types.
     * @param LLMS_Membership $this      Membership object.
     */
    $posts = apply_filters( 'llms_membership_get_associated_posts', $posts, $post_type, $this );
 
    // If a single post type was requested, return only that.
    if ( $post_type ) {
        // Return the request post type array and fallback to an empty array if that post type doesn't exist.
        return isset( $posts[ $post_type ] ) ? $posts[ $post_type ] : array();
    }
 
    // Remove empty arrays and return the rest.
    return array_filter( $posts );
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
4.15.0 Minor restructuring to only query post type data when it's needed.
3.38.1 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.