X

Cómo forzar que un Custom Post Type sea privado en WordPress

Snippets by IsItWP

¿Está buscando una manera de forzar su tipo de entrada personalizada a ser privada por defecto? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que se puede utilizar para forzar tipo de mensaje personalizado para ser privado en WordPress.

Instrucciones:

Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:

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

No olvide cambiar my_post_type por el nombre de su tipo de entrada personalizado. Este es un ligero cambio desde el código fuente y no incluye una contraseña ya que esto es privado y no protegido por contraseña.

Nota: Si es la primera vez que añade fragmentos de código en WordPress, consulte nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente su sitio.

Si te ha gustado este fragmento de código, por favor considere revisar nuestros otros artículos en el sitio como: 28 mejores temas de WordPress para curriculum vitae y cómo crear un formulario de solicitud de empleo en WordPress.

Comentarios   Deja una respuesta

  1. Hello,

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

    Thanks in advance

  2. Fabrizio Fabricatore julio 25, 2013 en 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 abril 5, 2012 en 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 febrero 15, 2011 en 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 […]

Añadir un comentario

Nos alegra que haya decidido dejar un comentario. Tenga en cuenta que todos los comentarios se moderan de acuerdo con nuestra política de privacidad , y que todos los enlaces son nofollow. NO utilice palabras clave en el campo del nombre. Tengamos una conversación personal y significativa.

WordPress Launch Checklist

La lista definitiva para lanzar WordPress

Hemos recopilado todos los elementos esenciales de la lista de comprobación para el lanzamiento de su próximo sitio web de WordPress en un práctico ebook.
Sí, envíeme el ¡gratuito!