• Hi guys,

    I am developing a members only site in which almost all posts need to have visibility = Private. Since WP makes them public by default, I looked for plugins but could only find 2 older plugins both of which I tried without luck.

    I then googled for a hack and found this thread which provided the below code (thanks Steve). It does almost exactly what is needed, i.e. posts are created as Private by default, however there is one problem: on the rare occasion I need a Public post, when I save the new post it forces it to be Private even though I set it to Public (so I guess it works too good :-).

    Would anyone have a tweak which could help fix this issue? The code I am using is:

    //Force posts of custom type 'internt-arkiv' to be private
    //…but first make sure they are not 'trash' otherwise it is impossible to trash a post
    function force_type_private($post)
    {
        if ($post['post_type'] == 'post') {
            if ($post['post_status'] != 'trash') $post['post_status'] = 'private';
        }
        return $post;
    }
    add_filter('wp_insert_post_data', 'force_type_private');

    Thanks in advance,

    Dennis

    • This topic was modified 5 years, 4 months ago by mrwassen.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • I think that Private won’t actually work for you. According to https://codex.wordpress.org/Post_Status
    Viewable only to WordPress users at Administrator level. (private)

    This plugin, however, is what you asked for:
    https://wordpress.org/plugins/private-post-by-default/
    You can read the code and see what’s different.
    But you’d be better off with one of the plugins that does membership sites for you, although they seem to focus on existing content and not user content.

    Thread Starter mrwassen

    (@dwl003)

    Hi Joy,

    Thanks for the quick response. Actually, when I setup new users with Subscriber or Editor role, they are able to view Private Posts. I have admittedly played around with a lot of plugins which may have modified the behavior of this, but to prove/disprove this, I will try to do a new clean install without plugins and test this.

    I do also see conflicting info on the topic:

    As you say, this document says administrators only: https://codex.wordpress.org/Post_Status

    however this document says Editor’s also:

    https://codex.wordpress.org/Content_Visibility

    In any case, I now have everything working correctly where my Subscribers and Editors can view the posts (except for the issue I am having with saving public posts).

    Also, I did try the plugin you quoted but couldnt get it to work. I will also try this in a clean new instance to see how it works.

    But I would still be curious to know if there is a tweak that could be done to the code in my original post?

    Thanks again,

    Dennis

    Your code is filtering right when the data is written to the database, so there’s no chance for the user to have the last word.
    I didn’t look, but I bet the code in the plugin mentioned sets the status at the beginning of editing the post, not at the end.
    (you don’t have to use the plugin, but it is good to look at the code)

    Thread Starter mrwassen

    (@dwl003)

    OK just looping back on this. I did a clean install, with no plugins and using the default theme. I setup a private post and can confirm that it can be viewed by users with the Editor role but not the Subscriber role, so I will have to drill down and find which plugin modified this behavior.

    With regards to the plugin you suggested, it worked fine on the clean install, then I removed the custom code and installed the plugin on the live site and that worked as well so not sure what I did wrong when I initially tested it (forgot to activate it perhaps??).

    In any case I think I am good for now.

    I appreciate your help as I am a bit of a newbie 🙂

    Thanks

    Dennis

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Posts to be Private by default’ is closed to new replies.