I have several unpublished posts in my WordPress website and I am trying to make it accessible for normal users (who are not logged in) using the normal post slugs (site.com/post-here
). I understand it may not be the best practice but for my special purpose, this needs to be done.
I have tried adding the following code snippet into my functions.php
file:
function enable_view_drafts() {
$role = get_role( 'subscriber' );
$role->add_cap( 'read_private_posts' );
$role->add_cap( 'edit_posts' );
}
add_action( 'after_setup_theme', 'enable_view_drafts');
I’ve also tried init
hook instead of after_setup_theme
. No luck.
My understanding is that changes to roles are saved to the database so only need to be done once. That is why I’m using after_setup_theme
hook to call the function.
But when I try to access the page as a normal user, I’m being shown a 404 page instead of showing the post content. I’ve also tried loading the preview URL (site.com/?p=212&preview=true
) but that didn’t work either.
These are my guesses:
- the normal user doesn’t have enough
caps
to read the drafts post. - testing and viewing draft posts on the front-end is not possible for any users (including administrators).
What changes do I have to make in order to accomplish what I’m trying to do? If it’s not possible, what alternative solutions do you suggest?
Note: I’m not looking for plugin-based solutions.