Viewing 2 reply threads
  • Author
    Posts
    • #2441

      I want to set all posts of a custom type to have a status of private by default. To do this, I’ve added this line to the metabox responsible for editing the post type.

      //Force post to always be hidden
      piklist('field', array(
          'type' => 'hidden'
          ,'field' => 'post_status'
          ,'value' => 'private'
      ));
      

      But it doesn’t seem to be changing the post status. Am I missing something here? Perhaps the setting for the post status in the publish metabox is overriding it?

    • #2442
      Steve
      Keymaster

      @wpkonsulterna– This is a great idea, but I don’t think it’s going to work unless you want to hide the default publish box and build a new dropdown with post status (i.e. you would need to add 'scope'=> 'post'.

      I found this old plugin, and with a few tweaks, I made it work with a custom post type. You may need to test further:

      function default_post_visibility()
      {
        global $post;
      
        if($post->post_type != 'MY_CUSTOM_POST_TYPE')
        {
          return;
        }
      
        if ('publish' == $post->post_status)
        {
            $visibility = 'public';
            $visibility_trans = __('Public');
        }
        elseif (!empty( $post->post_password))
        {
            $visibility = 'password';
            $visibility_trans = __('Password protected');
        }
        elseif ($post->post_type == 'MY_CUSTOM_POST_TYPE' && is_sticky($post->ID))
        {
            $visibility = 'public';
            $visibility_trans = __('Public, Sticky');
        }
        else
        {
            $post->post_password = '';
            $visibility = 'private';
            $visibility_trans = __('Private');
        }
        ?>
      
        
      
      
      		
      	
    • #6671

      Hi,

      I found a simple way to make it work.

      Define private status, as the first item of the array.
      Thus this becomes the default status.
      Once saved with private status, visibility also becomes private.

      **The code was inserted in the same file that registered the custom post type (main plugin file, or file include), and not in the file meta-boxes.

      add_filter(‘piklist_post_types’, ‘my_post_type’);
      function my_post_type($post_types)
      {
      $post_types[‘myposttype’] = array(
      .
      .
      .
      .

      ,’status’ => array(
      ‘private’ => array(
      ‘label’ => ‘Private’
      )
      ,’draft’ => array(
      ‘label’ => ‘Draft
      )
      )

      .
      .
      .
      return $post_types;
      }

Viewing 2 reply threads
  • You must be logged in to reply to this topic.