• Resolved Kubalonek84

    (@kubalonek84)


    Hi , is it possible to hide specyfic ID course by filter in function.php ? I try put code in lifterlms_before_loop_item.
    I want hide course from specyfic WP user role.

    many Thanks
    Wojtek

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Nick Mariano

    (@reddotinmotion)

    Hi @kubalonek84,

    You can use the PHP code below to hide some courses (by course ID) from the LifterLMS student dashboard page based on the specific WordPress user roles. In the code below, just replace $roles_to_exclude with the array of WordPress user roles that you want. This array can have one or more items in it. You also need to replace $posts_to_exclude with the array of course IDs.

    function exclude_posts_for_roles($query) {
        // Check if it is the main query and the user is logged in
        if (is_admin() || !$query->is_main_query() || !is_user_logged_in()) {
            return;
        }
    
        // Replace the array with your actual roles
        $roles_to_exclude = array(
    		'student',
    		'editor'
    	);
    
        // Replace the array with your actual post IDs to exclude
        $posts_to_exclude = array(
    		98,
    		109
    	);
    
        // Check if the user has any of the specified roles
        $user_has_excluded_role = false;
        foreach ($roles_to_exclude as $role) {
            if (current_user_can($role)) {
                $user_has_excluded_role = true;
                break;
            }
        }
    
        // If the user has any of the specified roles, exclude the specific posts
        if ($user_has_excluded_role) {
            $query->set('post__not_in', $posts_to_exclude);
        }
    }
    
    add_action('pre_get_posts', 'exclude_posts_for_roles');
    Thread Starter Kubalonek84

    (@kubalonek84)

    Hi Nick Mariano?, thanks for it. I test Your code and it works but posts are visible in loop, when I clik for link I have Page not found. It is possible to exclude this IP from loop ?
    Wojtek

    Plugin Support Nick Mariano

    (@reddotinmotion)

    Hi @kubalonek84,

    Thanks for confirming that the code partially works. We sometimes give custom code to users specially when we intend to add those snippets to our snippet library at github.com/gocodebox/snippets.

    Kindly note that providing customizations is outside the scope of our existing support policy. As a result, if you are actively working with or developing a theme or child theme, we recommend that you hire a WordPress developer for customizations like this.

    Another quick (and free) alternative to hiring a developer is to use AI tools like ChatGPT for general WordPress questions, since you can test out the code recommendations on your test site in real time as they are generated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘I want hide some courses by ID’ is closed to new replies.