WordPress.org

Ready to get started?Download WordPress

Forums

Sort posts by UPCOMING dates (7 posts)

  1. Lasha
    Member
    Posted 3 years ago #

    I need to replicate how a Calendar's "upcoming events" behaves, but with posts.

    This page will only display posts that match the current date, and future posts.

    Example: If today is June 1st, 2010, it will only show future-dated posts like June 10th, July 4th, August 12th, etc. It will NOT show any posts that are dated before June 1st, like May 10th or April 24th.

    I know this is more advanced than the average sort, but I hope this can be done!

  2. Lasha
    Member
    Posted 3 years ago #

    I found this bit of code which somewhat solves the problem. My guess is that this code goes in the functions.php file.

    How do I make it so this date filter only applies to posts being retrieved on a custom page template? I don't want it to effect the whole blog.

    http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters
    Return posts for posts for March 1 to March 15, 2009:

    <?php
    //based on Austin Matzko's code from wp-hackers email list
    function filter_where($where = '') {
    //posts for March 1 to March 15, 2009
    $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    I have a custom page that displays posts like so:
    http://codex.wordpress.org/Pages#A_Page_of_Posts

    With my scenario, I would only set the filter to "post_date >= the_date('Y-m-d')" but where does this go? How do you apply this filter to the custom $wp_query?

  3. forunner
    Member
    Posted 3 years ago #

    query_posts usage

    <?php
    
    //The Query
    query_posts('posts_per_page=5');
    
    //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
     ..
    endwhile; else:
     ..
    endif;
    
    //Reset Query
    wp_reset_query();
    
    ?>
  4. Lasha
    Member
    Posted 3 years ago #

    Where exactly would I add the filter in that block of code?

  5. forunner
    Member
    Posted 3 years ago #

    between
    //The Query
    and
    //The Loop

  6. Lasha
    Member
    Posted 3 years ago #

    Yes, I understand this, but doesn't the add_filter need to be in the functions.php page?

    If so, how do I pass the "post_date >= the_date('Y-m-d')" filter to the add_filter function, and make the query_posts recognize it?

  7. forunner
    Member
    Posted 3 years ago #

    I don't know :p
    maybe by calling the function
    filter_where();

Topic Closed

This topic has been closed to new replies.

About this Topic