Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know I can publicly show future posts in a loop using 'post_status' => 'future' in WP_Query. But clicking on a future post's permalink will result in a 404 if you are not a logged-in user.

Suppose I have a post called Apocalypse in the post_type 'event', scheduled for 12-12-2099. The permalink is mysite.com/event/apocalypse. Is it possible to make mysite.com/event/apocalypse and other future 'event' posts visitable now by the public?

Ideally I'd be able to restrict future post availability to the 'event' post type, but I would settle for a solution that makes all future posts available regardless of post_type.

share|improve this question
    
why do u need a future post to be viewable to non logged in users? I think i can come up with a solution but im just checking. –  Ijaas Oct 4 '11 at 3:32

1 Answer 1

up vote 2 down vote accepted

In short, you can make future posts visible by telling Wordpress to mark them as 'published' instead of 'scheduled'. You do this by using a future_post hook, which gets called when a post changes status. Each post type automatically gets its own future hook; since the custom post type I'm using is event, I can use the future_event hook. Here is the code:

function setup_future_hook() {
// Replace native future_post function with replacement
    remove_action('future_event','_future_post_hook');
    add_action('future_event','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');

This solution came from this SE question: Marking future dated post as published

A caveat with this approach

The caveat I will add is that this makes it more difficult to get a loop of just future posts. Before, I could simply use 'post_status' => 'future'; but now, since we've set future posts' post_status to published, that doesn't work.

To get around this you can use a posts_where filter on your loop (for example, see the date range examples on the codex here: http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters), or you can compare the current date to the post date, something like this:

    // get the event time
    $event_time = get_the_time('U', $post->ID);

    // get the current time
    $server_time = date('U');

    // if the event time is older than (less than)
    // the current time, do something
    if ( $server_time > $event_time ){
       // do something
    }

However, neither of these techniques is as easy as having a separate post_status for future posts. Perhaps a custom post_status would be a good solution here.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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