Include private posts from specific categories to the main query
-
Hello, how can i modify the main query so it would return posts as usual, but also include private posts related to specific category from which the user has permission to read private posts. In other words there are posts, some of them are private, and each one of them is related to a category, an authorized user somehow should be able to receive in the main query thouse private posts (alongside with non private posts) but only if they subscribed to this category.
Say I have a user with the role “Subscriber” and the custom capability “read_private_posts_from_categoty_economics”, which I add using this code, and i’m using ACF here:
add_action('profile_update', 'set_capabilities', 10, 3); function set_capabilities($user_id, $oldUserData, $newUserData) { $user = new WP_User($user_id); $categories = get_field('can_read_categories', 'user_' . $user_id); if ($categories) { foreach ($categories as $category) { $user->add_cap('read_private_posts_from_categoty_' . $category->slug); } } }
what would be the right way (WP way) to achieve this functionality, and if this even possible to do with what WP provides?
- The topic ‘Include private posts from specific categories to the main query’ is closed to new replies.