X

How to Force Custom Post Type to be Private in WordPress

Snippets by IsItWP

Are you looking for a way to force your custom post type to be private by default? While there’s probably a plugin for this, we have created a quick code snippet that you can use to force custom post type to be private in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function force_type_private($post)
{
    if ($post['post_type'] == 'my_post_type')
    $post['post_status'] = 'private';
    return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');

Do not forget to change my_post_type to the name of your custom post type. This is a slight change from the source code and does not include a password since this is private and not password protected.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 28 best WordPress resume themes and how to create a job application form in WordPress.

Comments   Leave a Reply

  1. Hello,

    Any idea how to do the same code but for 2 different CPT instead one?

    Thanks in advance

  2. Fabrizio Fabricatore July 25, 2013 at 8:33 am

    i know this thread is a little old but…how can you target a custom field in that custom post type to change it’s value?

  3. Martin Storbeck April 5, 2012 at 7:11 am

    I edited it, so links are set to the post name instead of “my_post_type/auto-draft” when they are created. This way you can also post password-protected posts.

    function force_type_private($post)
    {
        if ($post[‘post_type’] == ‘my_post_type’) {
            if ($post[‘post_status’] == ‘publish’) $post[‘post_status’] = ‘private’;
        }
        return $post;
    }
    add_filter(‘wp_insert_post_data’, ‘force_type_private’);

  4. This works, but I have found that it makes the post impossible to move to the trash… have you also experienced this?

    1. No I have not had this issue, but ill give it a try and see if I can replicate the problem.

    2. Yes, I’m having the exact same problem.. Anybody have any ideas?

  5. Why don’t you just do: if ( post type = my post type) { post status = private; return post; }

    Seems like you have an extra unneeded line.

    1. This is true Adam, fixed it.

  6. is this code correct?
    I think that you forgot one “ELSE”

    1. Hi Jose,
      No it is correct, just create a new custom post type then add this to the functions.php and you will notice when you select add new for that custom post type it will be default have private selected.

      1. Hello Kevin,

        Still no way to delete my posts with this code.Any idea ?

        Regards,

        1. I used code like this, which checks if they are in the trash before making them private:
          //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’] == ‘my_post_type’) {
                  if ($post[‘post_status’] != ‘trash’) $post[‘post_status’] = ‘private’;
              }
              return $post;
          }
          add_filter(‘wp_insert_post_data’, ‘force_type_private’);

  7. Tweets that mention Wordpress Force custom post type to be private – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com February 15, 2011 at 12:37 am

    […] This post was mentioned on Twitter by HTMLfrp, wp_freak. wp_freak said: #wordpress Force custom post type to be private http://bit.ly/fHzJuW […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!