Marking future dated post as published

Use case:

I have a post type that relates to a music show. The ‘show time’ is grabbed through the post publish date which is set to some time in the future. I’ve found a function that takes future dated blog posts and leaves the date but marks the post as published on save but haven’t been able to figure how to customize it for a custom post type.

Read More

The function is below.

<?php
    function setup_future_hook() {
        // Replace native future_post function with replacement
        remove_action('future_post','show', '_future_post_hook');
        add_action('future_post', 'show', 'publish_future_post_now');
    }

    function publish_future_post_now($id) {
        // Set new post's post_status to "publish" rather than "future."
        wp_publish_post($id);
    }

    add_action('init', 'setup_future_hook');
?>

Related posts

Leave a Reply

5 comments

  1. Awesome combining both the answers from Mike and Jan I came up with this which works only on the post type in question. We don’t need the conditional of show because the ‘future_show’ hook only grabs the post type of show and updates that.

    <?php
        function setup_future_hook() {
            // Replace native future_post function with replacement
            remove_action('future_show','_future_post_hook');
            add_action('future_show','publish_future_post_now');
        }
    
        function publish_future_post_now($id) {
            wp_publish_post($id);
        }
    
        add_action('init', 'setup_future_hook');
    ?>
    
  2. I can’t tell for sure since I don’t have your site to test with but I believe you just need to remove 'show' from remove_action() and add_action() calls. The wp_publish_post() function is agnostic with respect to post types, at least that’s what it appears from reading the code. So try this:

    <?php
        function setup_future_hook() {
            // Replace native future_post function with replacement
            remove_action('future_post', '_future_post_hook');
            add_action('future_post', 'publish_future_post_now');
        }
    
        function publish_future_post_now($id) {
            // Set new post's post_status to "publish" rather than "future."
            wp_publish_post($id);
        }
    
        add_action('init', 'setup_future_hook');
    ?>
    

    Of course if you want to limit to only doing shows you might want to do something like this (although the logic will be more complicated if you need it to work with other post types too):

    function publish_future_post_now($id) {
        $post = get_post($id);
        if ('show' == $post->post_type)
            wp_publish_post($id);
    }
    

    Hope this helps?

  3. I think this action gets called by wp_transition_post_status. The code is:

    function wp_transition_post_status($new_status, $old_status, $post) {
        do_action('transition_post_status', $new_status, $old_status, $post);
        do_action("${old_status}_to_$new_status", $post);
        do_action("${new_status}_$post->post_type", $post->ID, $post);
    }
    

    So for normal future posts the last hook will be future_post, but for your type it will be future_show or whatever your type slug is.

  4. If all you are wanting to do is add an extra date for ‘show times’, it may be easier to add a meta box which does exactly that – using the Publish date to spoof it could be potentially problematic, as WordPress will set it to “future” status whenever it is updated with a future publish date (I think), so you will have to be hooking every time a post is updated to set it back again. Also, that way you could reserve “Publish Date” for what it is intended.

    I would probably just use a meta_key, and a custom meta box. Though it depends how complex your queries are that show the posts.

    If you set the meta_value as a timestamp, you can order by the show time date still, or select before / after a certain date:

    $events = get_posts( 'post_type=events&meta_key=show_time&meta_value=' . strtotime( '+1 week' ) . '&meta_compare=<&orderby=meta_value' );

    Would get all “events” with a showtime no later then a week from the current date. Note: The above is untested, but should work 🙂

  5. WP Dev Nacin uses a simple function override:

    remove_action('future_post', '_future_post_hook');
    add_filter( 'wp_insert_post_data', 'nacin_do_not_set_posts_to_future' );
    
    function nacin_do_not_set_posts_to_future( $data ) {
        if ( $data['post_status'] == 'future' && $data['post_type'] == 'post' )
            $data['post_status'] = 'publish';
        return $data;
    }