• Resolved mayall79

    (@mayall79)


    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

    (@intoxstudio)

    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.