Title: User granted posts
Last modified: May 16, 2019

---

# User granted posts

 *  Resolved [mayall79](https://wordpress.org/support/users/mayall79/)
 * (@mayall79)
 * [7 years ago](https://wordpress.org/support/topic/user-granted-posts/)
 * Hi,
 * I want to create a loop in which the user can see the posts to which he has access.
 * Is it possible?

Viewing 1 replies (of 1 total)

 *  Plugin Author [Joachim Jensen](https://wordpress.org/support/users/intoxstudio/)
 * (@intoxstudio)
 * [7 years ago](https://wordpress.org/support/topic/user-granted-posts/#post-11582939)
 * There is currently no built-in way to do this, but there are 2 solutions:
 * 1. You can use the API in this way:
 *     ```
       if(rua_get_user(get_current_user_id())->has_level($level_id)) {
         //display post
       } else {
         //display nothing or teaser
       }
       ```
   
 * 2. Or, if you only restrict content by page or post ID, add this snippet to functions.
   php in your theme:
 *     ```
       add_action( 'pre_get_posts', function($query) {
       	if (!is_admin() && !$query->is_singular() && (!isset($query->query["post_type"]) || $query->query["post_type"] != RUA_App::TYPE_RESTRICT)) {
       		global $wpdb;
       		$other_levels = array_diff(
       			array_keys(RUA_App::instance()->get_levels()),
       			rua_get_user()->get_level_ids()
       		);
       		$result = $wpdb->get_col("SELECT m.meta_value FROM $wpdb->postmeta m INNER JOIN $wpdb->posts p ON m.post_id = p.ID WHERE m.meta_key = '_ca_post_type' AND p.post_parent IN ('".implode("','", $other_levels)."')");
       		if($result) {
       			$query->set('post__not_in', $result);
       		}
       	}
       	return $query;
       } );
       ```
   
 * This will remove restricted posts/pages/custom post types from blog page, search
   results, custom lists, etc.
 * Let me know if either of these works for you!

Viewing 1 replies (of 1 total)

The topic ‘User granted posts’ is closed to new replies.

 * ![](https://ps.w.org/restrict-user-access/assets/icon-256x256.png?rev=1815922)
 * [Restrict User Access - Ultimate Membership & Content Protection](https://wordpress.org/plugins/restrict-user-access/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/restrict-user-access/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/restrict-user-access/)
 * [Active Topics](https://wordpress.org/support/plugin/restrict-user-access/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/restrict-user-access/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/restrict-user-access/reviews/)

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 1 reply
 * 2 participants
 * Last reply from: [Joachim Jensen](https://wordpress.org/support/users/intoxstudio/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/user-granted-posts/#post-11582939)
 * Status: resolved