Make a particular category login complusory in WordPress

I have client require to show a particular of Categories only after login only..

I have got below code which is working for Post Id, but i need it for full categories..

Read More

Below is code :

function my_force_login() {
   global $post;
   if (!is_single()) return;
    $ids = array(188, 185, 171); // array of post IDs that force login to read
   if (in_array((int)$post->ID, $ids) && !is_user_logged_in()) {
    auth_redirect();
  }
 } 

How can i modified above code to work in for Category…

Related posts

1 comment

  1. I found a way from the code itself…

    function my_force_login() {
        global $post;
        $categories=get_the_category();
        $catID = $categories[0]->term_id;
        if (!is_category()) return;
    
        $ids = array(5); // array of post IDs that force login to read
        if (in_array((int)$catID, $ids) && !is_user_logged_in()) {
            auth_redirect();
        }
    } 
    

Comments are closed.