Site icon Hip-Hop Website Design and Development

How do I make a draft post accessible to everyone?

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:

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.