WordPress.org

Ready to get started?Download WordPress

Forums

A CHALLENGE - Future Timestamp Posts (15 posts)

  1. smijos
    Member
    Posted 8 years ago #

    WordPress refuses to display posts with timestamps in the future (as far as I can tell). Any way to change this?

    (purpose: Upcoming events category w/ date)

  2. Kafkaesqui

    Posted 8 years ago #

  3. smijos
    Member
    Posted 8 years ago #

    darn - no way w/out a plugin eh? ah well...

  4. lenwood
    Member
    Posted 8 years ago #

    Why the aversion to a plugin if this does what you're looking for?

  5. Kafkaesqui

    Posted 8 years ago #

    You could custom code a solution and use it in a category template for that particular category:

    <?php
    $postlimit = -1; // # of posts to display. -1 for all.

    $now = current_time('mysql');
    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat' AND post_id = ID AND post_date > '$now' LIMIT $postlimit");

    if($future_posts) : foreach($future_posts as $post) : setup_postdata($post);
    ?>

    ~ This is the post loop for future posts. ~

    <?php endforeach; else: ?>

    ~ This displays when there are NO future posts. ~

    <?php endif; ?>

    The above would replace The Loop in your template.

  6. smijos
    Member
    Posted 8 years ago #

    whoa...this is getting beyond me fast...

    I don't know a lot about category_template's but just one question - this little upcoming events thing is on my index.php.

    Will your solution still work? Or would it only work on that category's page?

  7. Kafkaesqui

    Posted 8 years ago #

    Just change the first part of the code to the following to run it anywhere (that is, add it to any template):

    <?php
    $cat_id = 1 // The category ID.
    $postlimit = -1; // # of posts to display. -1 for all.

    $now = current_time('mysql');
    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat_id' AND post_id = ID AND post_date > '$now' LIMIT $postlimit");

    Change $cat_id to your future post's category ID.

  8. smijos
    Member
    Posted 8 years ago #

    Kafkaesqui - you are scary brilliant! THANK YOU
    This is a VERY COOL little piece of code.
    I'm posting the final working version below

    <?php
    $cat_id = 16;
    $postlimit = -1;
    $now = current_time('mysql');
    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat_id' AND post_id = ID AND post_date > '$now' LIMIT $postlimit");
    if($future_posts) : foreach($future_posts as $post) : setup_postdata($post);
    ?>

    <-- the affectionately termed, "do stuff" -->

    <?php endforeach; else: ?>
    <?php endif; ?>

  9. diagonalizable
    Member
    Posted 8 years ago #

    This is a beautiful piece of code. Am I too late to ask questions about it?
    Hopefully someone is still out there to tell me how to tweak it so it displays TODAY's events as well as the future events.
    And the order they are being published seems random to me, can anybody help me with getting it in order earliest to latest?

    Thanks

  10. diagonalizable
    Member
    Posted 8 years ago #

    I figured out that you can display today's events if you roll back the date by replacing:

    $now = current_time('mysql');
    with
    $now = date('Y-m-d H:i:s',(time() - 86400));
    (where 86400 is the number of seconds in 24 hours)

    The order of the posts seems to be by post ID (except 11 is coming before 4 in one instance)
    Since this is not your typical query, how can I cheat to get ORDER BY in there?

    Anyone?
    You can see how it's working on my site http://www.uncoolkids.com
    I use EventCalender3 for the future postings, but this is the only way I can think of to get it to work for my category listings. Other ideas are also welcome.

  11. diagonalizable
    Member
    Posted 8 years ago #

    Never Mind, I just had my ORDER BY in the worng place.
    In case anybody else cares this will do the trick:

    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat_id' AND post_id = ID AND post_date > '$now' ORDER BY post_date LIMIT $postlimit");

    I'll shut up now

  12. contentrobot
    Member
    Posted 8 years ago #

    The EventCalendar3 plugin works well to show future events. One caveat when using WP 2.0 with a modified permalink structure (like /2006/08/03/sample-post/)... Make sure to apply the Patch for WordPress 2.0 described here http://blog.firetree.net/2006/01/31/eventcalendar-303/

  13. thornomad
    Member
    Posted 8 years ago #

    Hi all, thanks for all this work. I wondered if there was a way to reverse the order that the posts are output in this modified loop ?

    I have used the code above to create a FUTURE EVENTS category (that displays events in the future) and a PAST EVENTS category which displays the events <srong>from the FUTURE EVENTS category that have moved into the past (sort of like a dummy category, nothing actually gets posted there.

    However: it is showing the oldest event at the top of my PAST EVENTS list and I would rather it be at the bottom. I don't know enough about mysql to change that myself.

    If you could help would be great! Thanks.

  14. thornomad
    Member
    Posted 8 years ago #

    Nevermind, I found the solution to my problem (smile). One simply must add "DESC" after the ORDER BY post_date call.

    So, here is what I am using in my "dummy" category (which has one post set to 2999 so that it shows up in my category lists. Category Two is my "future events" category.


    $cat_id = 2; // The category ID of "future posts" category
    $postlimit = 1000; // # of past posts to display.

    $now = date('Y-m-d H:i:s',(time() - 86400)); // allow one day leeway (86400 is # seconds in 24hrs)

    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat_id' AND post_id = ID AND post_date < '$now' ORDER BY post_date DESC LIMIT $postlimit"); //note the reversed GREATER THAN symbol

    if($future_posts) : foreach($future_posts as $post) : setup_postdata($post);

  15. KENpress
    Member
    Posted 7 years ago #

    Hello. I am hoping someone may still be watching this discussion.

    This future posts code is amazing. I am using it in the form Kafkaesqui gave in his second post.

    $cat_id = 16;
    $postlimit = -1;
    $now = current_time('mysql');
    $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts, $wpdb->post2cat WHERE category_id = '$cat_id' AND post_id = ID AND post_date > '$now' LIMIT $postlimit");

    It has helped me so much, but I am having a problem. If I am not logged into my wordpress admin, the permalinks to my future dated posts stop working. If I am logged in, everything works perfectly, but after I have logged out, if I click on a future dated posts permalink, I get the search form and a "Sorry, but you are looking for something that isn't here." Suddenly the archive page will no longer display the post unless its date has passed.

    Log back in and it works fine.

    Is this to be expected? I am hoping for anyone who is viewing my site to see these future dated posts without being logged in. Is this a problem, or am I missing something?

    Thanks for any help. I really appreciate all of the knowledge available in here.

Topic Closed

This topic has been closed to new replies.

About this Topic