14

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.

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');
?>
1
  • 1
    Is it possible you have an error in your remove_action and add_action call? Your second parameter is 'show', but shouldn't it be the function you are adding or removing?
    – Jan Fabry
    Commented Aug 12, 2010 at 12:36

5 Answers 5

8

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

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
  • Yeah the first bit of code is what I started with and it works on normal WordPress posts but if you add the conditional of 'show' it doesn't work on anything. Even without the conditional it doesn't work on a custom post type. Commented Aug 12, 2010 at 21:47
  • I've read your question several times but it's still unclear to me exactly what you are trying to accomplish if we ignore the suggested code that doesn't work. Can you clarify what exactly you are trying to accomplish? Do you want to publish the post on the date/time that is in the post_date, or something else? Or have you already solved it? Commented Aug 12, 2010 at 22:16
  • It's already solved (I provided code below). I just wanted to publish the custom post type post even if scheduled for the future. Commented Aug 13, 2010 at 2:45
4

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.

1

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 :)

3
  • I agree with this 100% - much better to use a meta box than using the publish date as a hack for the event date. Commented Aug 12, 2010 at 15:53
  • Yeah a meta_box would work but I found it difficult to add a nice jQuery date and time picker. The stock WP one was way nicer than anything I could come up with. Commented Aug 12, 2010 at 22:06
  • WP Easy Post Types plugin gives a custom date field and provides a simple date picker, I've used it for an Events listing function myself. Commented Aug 16, 2010 at 21:47
0

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;
}
1
  • Please add some explanation how this works. Commented Feb 3, 2014 at 10:20

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