Tell us what you are trying to do?
Is it possible to create a blog view which shows only own user posts? Each user must create posts, but only be able to see their own, never others.
Is there any documentation that you are following?
Yes, documentation here.
Is there a similar example that we can see?
I don't know.
You may want to try the Toolset Access plugin, which allows for controlling the permissions of different user roles in relation to post viewing, editing, and publishing. You can find more information about it here: https://toolset.com/course-lesson/setting-access-control/
With this method, posts created by others won't be visible in the front end although they will still appear in the admin area list, only they won't be editable.
If you're interested in concealing posts from other authors in the admin area list as well, you can try the following code snippet from this tutorial: hidden link
I hope this information is useful. For more personalized guidance on custom code or related queries, you might also consider consulting the services of a professional from our recommended contractors list: https://toolset.com/contractors/
That's right, Toolset Access restricts access by user roles rather than individual user logins.
In your case, where each user must be able to view only their own posts on the frontend and not others, you would likely need a more custom solution. This typically involves creating a custom query or filtering mechanism in your theme or using a custom plugin to achieve this functionality.
You can try using the following code to limiting the posts based on specific users:
function display_user_posts_only($query) {
if (is_user_logged_in() && $query->is_main_query()) {
// Get the current user's ID
$current_user_id = get_current_user_id();
// Check if the current user is an administrator
if (!current_user_can('activate_plugins')) {
// If the current user is not an administrator, modify the query to show only their posts
$query->set('author', $current_user_id);
}
}
}
add_action('pre_get_posts', 'display_user_posts_only');
This code will:
- Check if a user is logged in and if it's the main query on the frontend.
- Get the current user's ID using get_current_user_id().
- Check if the current user is an administrator using current_user_can('activate_plugins'). Administrators typically have the capability to activate plugins, so this capability is commonly used to identify administrator users.
- If the current user is not an administrator, the query is modified to show only their posts in the frontend
Please give it a try and tell us how that goes for you.