Display closest post to current date

  • Unknown's avatar

    Hello,

    I was wondering if anyone could help me?

    I am using this code to display the next post after a certain date:

    <?php
    $gig = get_posts('numberposts=-1&orderby=post_name&order=ASC');
    foreach($gig as $post) : setup_postdata($post);
    endforeach;
    $args = array('numberposts' => '1');
    $recent_posts = wp_get_recent_posts($args);
    foreach($recent_posts as $recent){
    $date = get_post_meta($post->ID, "Date", $single = true);
    $town = get_post_meta($post->ID, "Town", $single = true);
    ?>
    
    <div id="latest-gig">
    <?php if (date('d-m-Y') > $date){?>
    <h2>Latest Gig</h2>
    <?php;}else{?>
    <h2>Upcoming Gig</h2>
    <?php } ?>
    <div id="latest-gig-box">
    <?php
    
    echo '<a href="' . get_permalink($recent["ID"]) . '" title="'.$recent["post_title"].'" id="gig-link"></a>';
    echo '<h3>' .   $recent["post_title"].'</h3>';
    echo '<br /><h4>' . $town . '<br/>' . $date . '</h4><br />';
    }
    ?>

    If I have two posts the query takes the top of the bunch (or bottom) depending on order.

    How would I retrieve the closest post after the current date?

  • Unknown's avatar

    You did not specify a blog address or reason for posting when you created this topic.

    This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you’ll find help at the WordPress.org forums.

    If you don’t understand the difference between WordPress.com and WordPress.org, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.

    This is an automated message.

  • The topic ‘Display closest post to current date’ is closed to new replies.