• ninsan

    (@jennyrigsjo)


    Hello,

    Thanks for a great plugin! I use a custom taxonomy called ‘visibility’ on my site to manage post visibility for logged-in and not-logged-in users. When a post has the term ‘loggedin’ associated with it I would like to exclude that post from being listed in the recent posts widget if the user is not logged in. Something like this:

    add_action('after_setup_theme', 'jr_cptw_setup_hooks');
    
    function jr_cptw_setup_hooks() {
      add_filter('custom_post_type_widgets/recent_posts/widget_posts_args', 'jr_cptw_loggedin_users_only');
    }
    
    /**
     * Hide recent posts with the taxonomy term 'loggedin' from logged-out users.
     */
    function jr_cptw_loggedin_users_only($args) {
    
        if (!is_user_logged_in()) {
            $taxonomy_query = array(
                        'taxonomy' => 'visibility',
                        'terms' => 'loggedin',
                        'field' => 'slug',
                        'operator' => 'NOT IN',
                    );
    
            $args->set('taxonomy_query', $taxonomy_query);
        }
    
        return $args;
    }

    Is it at all possible to do this? If so, how would I go about it?

    Cheers,
    Jen

    • This topic was modified 4 years, 3 months ago by ninsan.
  • The topic ‘Exclude posts with a particular taxonomy term from recent posts?’ is closed to new replies.