4

I have a custom post type and a completely custom user interface to access it, and would like these posts to have a publish status. I know how to make them skip trash and be deleted immediately, but this still leaves me with draft posts and private posts.

Is there a way to prevent posts getting flagged as drafts or private? I can hide the controls in CSS but I want to do this in code to make sure it isn't possible, is there a handy filter I can use?

2
  • 1
    I suppose I would add a check to the save_post/wp_insert_post filter to disallow or making it impossible to save this CPT as draft/private. Jun 2, 2014 at 13:20
  • I guess I actually might have missed something the first time around..what about the post_submitbox_misc_actions filter for example? Jun 2, 2014 at 14:09

1 Answer 1

3

I wonder if you could use something as simple as this: (similar to the suggestion of @ialocin)

/**
 * Use the "Force" on the post status ;-)
 */

add_action( 'wp_insert_post_data', function( $data, $postarr ){

    $change_post_status = array( 'draft', 'private' ); // Edit to your needs

    if( 'cpt' === $data['post_type'] 
         && in_array( $data['post_status'], $change_post_status, TRUE ) 
    )
        $data['post_status'] = 'publish';

    return $data;

}, PHP_MAX_INT, 2 );

where we just force the post status value when it's part of the $change_post_status array - before the post is saved.

(... or maybe I'm misunderstanding the setup ;-)

3
  • 1
    curious, there is the potential danger of duplicate posts if it tries to save revisions or drafts, I will have to test
    – Tom J Nowell
    Jun 2, 2014 at 16:24
  • If you spot something, we could then try to find a workaround with comparison to $postarr, for example.
    – birgire
    Jun 2, 2014 at 16:52
  • Hey @TomJNowell, I had forgotten about this problem and got curious if you did some further testing?
    – birgire
    Jul 28, 2014 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.