I made a small adjustment to that code to make it test if the CPT is from the type 'Page' before limiting the access:
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 or the post type is 'page'
if (!current_user_can('activate_plugins') || $query->get('post_type') === 'page') {
// If the current user is not an administrator or it's a 'page', don't modify the query
return;
}
// If the current user is not an administrator and it's not a 'page', modify the query to show only their posts
$query->set('author', $current_user_id);
}
}
add_action('pre_get_posts', 'display_user_posts_only');
Can you please test it and tell us if it has the expected effect?
I worked in following code, tested it locally and it is working as expected:
function restrict_post_access_and_list($query) {
// Check if the post type is "page." Allow access to all users for pages.
if ($query->is_page) {
return;
}
// If it is not CPT "page" and user is not logged in, send them to the login page
if (!is_user_logged_in()) {
wp_redirect(wp_login_url(get_permalink())); // Redirect non-logged-in users to the login page.
exit;
}
if (is_admin() || !$query->is_main_query()) {
return;
}
// Check if the user is an administrator.
if (current_user_can('administrator')) {
return;
}
// Check if the query is for a single post.
if ($query->is_single) {
$post_id = $query->get('p'); // Get the post ID.
$post = get_post($post_id); // Get the post object using the post ID.
if ($post && is_user_logged_in() && $post->post_author == get_current_user_id()) {
return;
} else {
wp_redirect(home_url()); // Redirect to the homepage for unauthorized users.
exit;
}
}
// Check if the query is for an archive or loop.
if ($query->is_home() || $query->is_archive()) {
$current_user_id = get_current_user_id();
$query->set('author', $current_user_id);
}
}
add_action('pre_get_posts', 'restrict_post_access_and_list');
We wanted to let you know that while we have provided some initial assistance with your website, our support is limited to certain scope and guidelines.
We have helped with a some custom code snippet for your WordPress website to address your specific requirements.
It's important to understand that custom coding, debugging, and any further modifications to the code are considered out of the scope of our support. While we are here to provide initial guidance and assistance, any additional development or customization needs may require the expertise of a professional contractor.
To assist you further with your project, we recommend considering the services of a skilled contractor who specializes in WordPress development. You can find experienced WordPress contractors available for hire through the following link: (https://toolset.com/contractors/).
These contractors have the expertise and experience to provide tailored solutions to meet your specific requirements and ensure the long-term success of your website.
We hope that the code snippet we provided serves as a helpful reference for your project and that you can find the right contractor to assist you further. If you have any questions or need additional guidance with using Toolset, please feel free to reach out, and we'll do our best to assist you.