TransWikia.com

Prevent private post 404

WordPress Development Asked by Ben Everard on December 15, 2021

I want private posts (for those not logged in) to use the single.php template just as public posts do, at the moment they get thrown over to the 404.php template.

I’ve had a good look through the core files but I cannot find where the template change (note: this is not a redirect) happens.

Can anyone help?

Edit

To explain why I want this, I want all visitors to see a preview of the content, but logged in “members” to see the entire post content.

2 Answers

You are not seeing a redirect, to confirm your own observation, so much as you are seeing just what it looks like-- a "File Not Found". Take a look at the query in the source around line 2438 (among others). The status of the post is part of the query itself, so if the status doesn't match then no posts are found and that means 404. As you can see from the code, status is pretty rigidly protected. That is proving difficult to override.

I'd recommend using a custom post type for you "logged in only" posts. That is going to be much easier to control both on the front end and the back end.

Answered by s_ha_dum on December 15, 2021

According to the Content Visibility page, Private posts are visible only to those with sufficient permission levels. WordPress doesn't expose them to non-logged-in users at all, hence (presumably) the 404.

If you want to display a limited preview to non-members, instead of setting the post to Private, you could try something like this:

add_filter( 'the_content', 'wpse101968_preview' );
function wpse101968_preview( $content ) {
    if( is_single() ) {
        if( ! is_user_logged_in() ) {
            $content = get_the_excerpt();
        }
    }
    return $content;
}

And then your non-logged-in visitors will see the post's excerpt instead of the entire post. You can customize the excerpts, too, so that they see only what you want them to see.

References

Codex:

Answered by Pat J on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP