Membership Plugin

WordPress Membership Plugin

  • Home
  • Documentation
  • Addons
  • Support
    • Quick Setup
    • Documentation
    • Premium Addon Support
    • Paid Support
    • Support Forum
    • Support Forum Search
    • Forum Login
    • Forum Registration
  • Contact

Hide protected post links from unregistered users

Simple Membership Plugin › Forums › Simple Membership Plugin › Hide protected post links from unregistered users

  • This topic has 0 replies, 1 voice, and was last updated 11 years, 7 months ago by truburt.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • January 20, 2015 at 7:19 pm #849
    truburt
    Participant

    If anyone else needs it, I resolved it this way:

    1. Created a new WordPress child theme
    2. Added functions.php with the following content:

    <?php
    
    if ( class_exists('SimpleWpMembership') ) {
        function swpm_get_protected_posts() {
            global $wpdb;
            $protected_posts = array();
    
            if (!is_admin()) {
                $level_id = -1;
        
                $auth = BAuth::get_instance();
                if ($auth->is_logged_in()) {
                    $level_id = $auth->get('membership_level');
                }
        
                $results = $wpdb->get_results("SELECT id, post_list FROM {$wpdb->prefix}swpm_membership_tbl");
        
                $allowed_posts = array();
                $all_posts = array();
                foreach ( $results as $result ) {
                    if ( isset($result->id) && isset($result->post_list) ) {
                        $posts = (array) unserialize($result->post_list);
                        if ($result->id == $level_id)
                            $allowed_posts = $posts;
                        else
                            $all_posts = array_merge($all_posts, $posts);
                    }
                }
                $all_posts = array_unique($all_posts);
        
                $protected_posts = array_diff($all_posts, $allowed_posts);
            }
    
            return $protected_posts;
        }
    
        add_action('pre_get_posts', 'swpm_exclude_protected_posts');
        function swpm_exclude_protected_posts($query) {
            $protected_posts = swpm_get_protected_posts();
            if (!empty($protected_posts)) {
                $query->set('post__not_in', $protected_posts);
            }
        }
        
        add_filter('get_previous_post_where', 'swpm_filter_adjacent_posts');
        add_filter('get_next_post_where', 'swpm_filter_adjacent_posts');
        function swpm_filter_adjacent_posts( $where ) {
            $protected_posts = swpm_get_protected_posts();
            if (!empty($protected_posts)) {
                $where .= " AND p.ID NOT IN (" . implode(',', $protected_posts) . ")";
            }
            return $where;
        }
        
    }

    This code adds two hooks:
    1. pre_get_posts hook allows to exclude restricted posts from menus, category listings, etc.
    2. get_previous_post_where and get_next_post_where excludes restricted posts from ‘Previous Post’/’Next Post’ links

  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

Please read this message before using our plugin.

Search

Featured Addons and Extensions

  • Membership Form Builder Addon
  • Member Directory Listing Addon
  • WooCommerce Payment Integration
  • Member Data Exporter Addon

Documentation

  • Documentation Index Page

Copyright © 2026 | Simple Membership Plugin | Privacy Policy