WordPress.org

Ready to get started?Download WordPress

Forums

Show today and future posts (3 posts)

  1. xanuex
    Member
    Posted 2 years ago #

    Hi guys,

    First of all I hope this is the right topic cause I couldn't place it in WP-advanced..

    I've found a lot of info about this already but I've stumbled upon a problem. I'm trying to show a list of posts from the database with a date equal to today or in the future.

    This is the code I use:

    -- functions.php

    function my_posts_where_from_today( $where = '' ) {
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
    	return $where;
    }
    add_filter( 'posts_where', 'my_posts_where_from_today' );

    -- index.php

    <?php query_posts( array( 'category_name' => 'games', 'post_status' => 'future || publish', 'post_type' => 'post', 'post_per_page' => '15', 'orderby' => 'date', 'order' => 'ASC' ) ); ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    
      //junk
    
    <?php endwhile; ?>
    
    <?php wp_reset_query(); ?>

    My problem:
    The code works like a charm but the filter is applied to every query. It seems logical but not really what I want.

    How can I apply the above on just this query?

    Thanks in advance,
    Marcel

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    leave the functions code in functions.php;
    but remove this line from functions.php:

    add_filter( 'posts_where', 'my_posts_where_from_today' );

    and add it before your 'query_posts()' line; i.e.

    add_filter( 'posts_where', 'my_posts_where_from_today' );
    <?php query_posts( array( 'category_name' => 'games', 'post_status' => 'future || publish', 'post_type' => 'post', 'post_per_page' => '15', 'orderby' => 'date', 'order' => 'ASC' ) ); ?>

    then, after the 'endwhile' line, add:

    remove_filter( 'posts_where', 'my_posts_where_from_today' );

  3. xanuex
    Member
    Posted 2 years ago #

    Thanks!
    I tried the first part but forgot to put the remove_filter afterwards :)

Topic Closed

This topic has been closed to new replies.

About this Topic