Title: Query posts by access level
Last modified: August 21, 2016

---

# Query posts by access level

 *  Resolved [duncan](https://wordpress.org/support/users/flowevd/)
 * (@flowevd)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-posts-by-access-level/)
 * I would like to be able to write the following code:
 *     ```
       get_posts( [ 's2_access_level' => 2 ]);
       ```
   
 * and get back all the posts at that access level. Failing that, I’d settle for
   this.
 *     ```
       s2_set_access_level(2);
       get_posts();
       ```
   
 * Are either of these things possible?
 * Thanks!
 * [https://wordpress.org/plugins/s2member/](https://wordpress.org/plugins/s2member/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [duncan](https://wordpress.org/support/users/flowevd/)
 * (@flowevd)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-posts-by-access-level/#post-5082051)
 * Perhaps I could create a dummy user with Level #2 access and pass them into `
   get_unavailable_singular_ids_with_ccaps( $user )`
    then use those ids in post__not_in?
 * **EDIT** That won’t work…. I need levels, not custom capabilities
 *  Thread Starter [duncan](https://wordpress.org/support/users/flowevd/)
 * (@flowevd)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/query-posts-by-access-level/#post-5082080)
 * OK, here’s how to do it (only works for posts, and not tested yet).
 *     ```
       function get_s2_restricted_posts_by_access_level( $level ) {
           $ids = array();
           $s2_options = get_option( 'ws_plugin__s2member_options' );
           for( $i = 0; $i <= $level; $i++ ) {
               if( $s2_options["level_$i_posts"] ) {
                   $ids = array_merge( $ids, explode( ',', $s2_options["level_$i_posts"] ) );
               }
           }
           return $ids;
       }
       ```
   
 * then
 *     ```
       $posts_visible_for_level_2 = get_s2_restricted_posts_by_access_level(2);
       get_posts( [ 'post__in' => $posts_visible_for_level_2 ] );
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Query posts by access level’ is closed to new replies.

 * ![](https://ps.w.org/s2member/assets/icon-256x256.png?rev=980067)
 * [s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions](https://wordpress.org/plugins/s2member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/s2member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/s2member/)
 * [Active Topics](https://wordpress.org/support/plugin/s2member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/s2member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/s2member/reviews/)

## Tags

 * [Access Level](https://wordpress.org/support/topic-tag/access-level/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * 2 replies
 * 1 participant
 * Last reply from: [duncan](https://wordpress.org/support/users/flowevd/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/query-posts-by-access-level/#post-5082080)
 * Status: resolved