Support » Fixing WordPress » Display next weeks posts

  • Hi, i found several topics in this forum about a WP_Query showing future posts, within a date range, etc… But none of these really helped me out or made my thing work.

    what i’d love to have is:

    results that display POSTS that are scheduled the next 7 DAYS

    🙂 yeah. that’d it. sounds simple, actually. but i don’t get it.

    thanks for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • this might work:

    this section would go into functions.php of your theme:

    // Create a new filtering function that will add our where clause to the query
    function filter_where_next_week( $where = '' ) {
    	// scheduled posts for next 7 days
    	$where .= " OR post_type='post' AND post_date > '" . date('Y-m-d') . "' AND post_date <= '" . date('Y-m-d', strtotime('+7 days')) . "'";
    
    	return $where;
    }

    this part would go into a template:

    <?php add_filter( 'posts_where', 'filter_where_next_week' );
    $query = new WP_Query(  $query_string . '&post_status=future' );
    if( $query->have_posts() ) :
    while( $query->have_posts() ) :
      $query->the_post();
      the_title();
      the_date();
    endwhile;
    else : echo 'nothing scheduled in next week';
    endif; wp_reset_postdata();
    remove_filter( 'posts_where', 'filter_where' );
    ?>

    not widely tested.

    http://codex.wordpress.org/Class_Reference/WP_Query#Time_Parameters

    Thread Starter flabrea

    (@flabrea)

    Wow that was fast! And the best part: it works! Thanks alchymyth! Thanks so much!

    Thread Starter flabrea

    (@flabrea)

    one more thing 🙂 is there a chance to output the number of results from this query? if 5 posts are found > 5 / if 3 are found > 3 / and so on?

    do you mean you want to show all posts that are is found?

    try:

    $query = new WP_Query(  $query_string . '&post_status=future&posts_per_page=-1' );
    Thread Starter flabrea

    (@flabrea)

    no, only to the number of posts that are found.
    eg. we have found 8 posts for the within the next 7 days.

    no, only to the number of posts that are found.

    yes, that what the posts_per_page=-1 is for; -1 means: show all that are found.

    I haven’t tested my latest suggestion – have you tried it?

    Thread Starter flabrea

    (@flabrea)

    sorry, actually i just want the NUMBER of posts to be display, not the content of those posts.

    now I am getting it now ;-(

    try:

    $query = new WP_Query(  $query_string . '&post_status=future&post_per_page=-1' );
    echo 'fp'.$query->found_posts;
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Display next weeks posts’ is closed to new replies.