Compare post levels and user levels wishlist member

I’m using Wishlist Member plugin and I’m building a function which compares user_levels and post_levels, but I can’t get my function to work:

check_user_access($userid, $postid){

     // get user_levels
     $user_levels = WLMAPI::GetUserLevels($userid);

     // get the post levels
     $post_levels = WLMAPI::GetPostLevels($postid);

     // Compare user_levels with post_level
     $result = array_search($user_levels, $post_levels);

     if ($result === false) {
         return false;
     }
     else {
         return true;
     }

 }

Related posts

Leave a Reply

3 comments

  1. Maybe because $user_levels is an array?

    Try something like:

    // Compare user_levels with post_level
    $result = '';
    foreach($user_levels as $user_level) {
        $result .= array_search($user_level, $post_levels);
    }
    if ($result) {
        return true;
    }
    
  2. I think this is what you’re looking for. This will let you find if a user can access a post, page, or category. This uses the new API functions in the latest versions of WishList Member and accepts ‘post’, ‘page’, or ‘category’ as the $object_type parameter.

    function member_can_access($user_id, $object_type, $object_id) {
        $levels = wlmapi_get_member_levels($user_id);
    
        $map = array(
            'post' => 'wlmapi_get_level_posts',
            'page' => 'wlmapi_get_level_pages',
            'category' => 'wlmapi_get_level_categories'
        );
    
        $plurals = array(
            'post' => 'posts',
            'page' => 'pages',
            'category' => 'categories'
        );
    
        foreach ( $levels as $level ) {
            $objects[] = call_user_func($map[$object_type], $level->Level_ID);
        }
    
        foreach ( $objects as $object ) {
            foreach ( $object[$plurals[$object_type]][$object_type] as $item ) {
                $items[] = $item['ID'];
            }
        }
    
        $items = array_unique($items);
    
        if ( !in_array( $object_id, $items ) ) {
            return false;
        }
    
        return true;
    }
    
    ?><pre><?php print_r(member_can_access(1, 'post', 1)); ?></pre>
    

    You can see the documentation for all the used functions at http://codex.wishlistproducts.com