wordpress snippets last updated   : April 14, 2014
we now have 630 snippets

Force custom post type to be private

wordpress snippet

Adding this PHP snippet to the functions.php of your wordpress theme will force your custom post type to be private by default. Do not forget to change my_post_type to the name of your custom post type. This is a slight change from the source snippet and does not include a password since this is private and not password protected.

snippet :  PHPcopy
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');
source →
  • Pingback: Tweets that mention Wordpress Force custom post type to be private – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com

  • Jose

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

    • http://wpsnipp.com Kevin Chard

      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.

      • Amine SOUSSI

        Hello Kevin,

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

        Regards,

        • Steve

          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’);

  • http://adamcap.com/ Adam Capriola

    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.

    • http://wpsnipp.com Kevin Chard

      This is true Adam, fixed it.

  • Steve

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

    • http://wpsnipp.com Kevin Chard

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

  • http://twitter.com/hdready Martin Storbeck

    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’);

  • Fabrizio Fabricatore

    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?

jqsnipp.com | we just launched jquery snippets website