1

I've registered a custom post status 'Editing'. Whenever a post is changed to use that status, the Publish time is set to the time the status was changed rather than 'Immediately'.

This is frustrating because my editors often don't notice this and publish posts 2 days in the past.

Here's the code:

// Register Custom Post Status
function custom_post_status() {

    $args = array(
        'label'                     => _x( 'Editing', 'Status General Name', 'futurism' ),
        'label_count'               => _n_noop( 'Editing (%s)',  'Editing (%s)', 'futurism' ),
        'public'                    => false,
        'internal' => true,
        'private' => true,
        'protected' => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'exclude_from_search'       => true,
        );
    register_post_status( 'editing', $args );

}
add_action( 'init', 'custom_post_status' );

// Add the option to the select menu in wp-admin because Wordpress doesn't do this for you
function append_post_status_list(){
    global $post;
    $complete = '';
    $label = '';
    if($post->post_status == 'editing'){
        $complete = ' selected=\"selected\"';
        $label = '<span id=\"post-status-display\"> Editing</span>';
    }
    echo '
    <script>
        jQuery(document).ready(function($){
            $("select#post_status").append("<option value=\"editing\" '.$complete.'>Editing</option>");
            $(".misc-pub-section label").append("'.$label.'");
        });
    </script>
    ';
}
add_action('admin_footer-post.php', 'append_post_status_list');

1 Answer 1

2

Few notes

  • We can get the "Publish immediately" notice within the post_submit_meta_box(() when the post_date_gmt is equal to '0000-00-00 00:00:00'.

  • We can see here within wp_update_post() and here and here within wp_insert_post(), how draft-, pending- or auto-draft posts are not date stamped.

  • It looks like if we had a filter on the post status array here and here that we could easily fix this for the custom editing status. I also wonder if it would make sense to control this from the register_post_status() setup?

  • It should be possible to modify the post_date_gmt within the wp_insert_post_data filter. Here we might try to get the previous post status from the hidden_post_status or hidden_post_status $postarr fields.

  • The handy transition_post_status hook, fires before the post is updated but before the wp_insert_post_data filter. It will therefore not work to adjust the corresponding database value at this stage, because it will just be overriden shortly after.

  • The post_updated hook fires after the post is updated. This hook provides an easy way to compare the post object before and after the update.

Example

Here's one example how we can hook into post_updated to clear the post_date_gmt for the post, when the new status is editing and the previous is neither publish or editing:

add_action( 'post_updated', function( $post_ID, $post_after, $post_before ) use ( &$wpdb )
{
    if( 
           'editing' === $post_after->post_status
        && ! in_array( $post_before->post_status, [ 'editing', 'publish' ]  )
        && 'post' === $post_after->post_type
    )
        $wpdb->update( 
            $wpdb->posts, 
            [ 'post_date_gmt' => '0000-00-00 00:00:00' ], 
            [ 'ID' => $post_ID ] 
        );      

}, 10, 3 );

Hopefully you can test this further and adjust to your needs.

1
  • Wow, thank you so much for the in-depth answer! People like you make the world a better place :)
    – jetlej
    Jul 1, 2016 at 17:39

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.