• Hi there..

    I am trying to bring the posts for a specific date (I have more than for that day). I spent a lot of time in the forums and found some solutions.

    The code below looks like working:

    <?php
    $current_year = date('Y');
    $current_month = date('m');
    $post_query = 'cat='.$cat1_id.'&posts_per_page=3&offset=1&year=$current_year&monthnum=$current_month&day=15';
    ?>
    <?php query_posts( $post_query ); ?>

    But when i change the specific day (19) with current day function, it fails to work:

    <?php
    $current_year = date('Y');
    $current_month = date('m');
    $current_day = date('j');
    $post_query = 'cat='.$cat1_id.'&posts_per_page=3&offset=1&year=$current_year&monthnum=$current_month&day=$current_day';
    ?>
    <?php query_posts( $post_query ); ?>

    I tried to debug and used <?php echo $current_day; ?> and it works flawlessly, shows the proper day (19).

    I have to use the function to grab the current day… But somehow it does not work.

    Do you have any idea about what i am missing?

    Thx

Viewing 9 replies - 1 through 9 (of 9 total)
  • Try converting your date strings into integers.

    Thread Starter LazySpidey

    (@lazyspidey)

    What do you mean by that?

    caused by false php string and variable concatenation;

    try:

    $post_query = 'cat='.$cat1_id.'&posts_per_page=3&offset=1&year='.$current_year.'&monthnum='.$current_month.'&day='.$current_day;

    The PHP date() function creates strings – not integers. According to the Codex, the time parameters in a custom query need to be integers.

    Thread Starter LazySpidey

    (@lazyspidey)

    Wohooo!!! Thx alchymyth!!! Works like a charm..

    For the ones who wants to query specific date (today’s) posts, can use the following code:

    <?php
    				$current_year = date('Y');
    				$current_month = date('m');
    				$current_day = date('j');
    
    $post_query = 'cat='.$cat1_id.'&posts_per_page=3&offset=1&year='.$current_year.'&monthnum='.$current_month.'&day='.$current_day;
    ?>
    				<?php query_posts( $post_query ); ?>

    Thx again!!

    alternatively, check the Codex:

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

    Returns posts for just the current date:

    $today = getdate();
    $query = new WP_Query( 'year=' . $today["year"] . '&monthnum=' . $today["mon"] . '&day=' . $today["mday"] );

    Thread Starter LazySpidey

    (@lazyspidey)

    OK.. But what about post after a specific date? Sould;

    $post_query = 'cat='.$cat1_id.'&posts_per_page=3&offset=1&year='.$current_year.'&monthnum='.$current_month.'&day>'.$current_day;

    work? (Noticed the “>” at the day variable?

    Because it doesn’t πŸ™‚

    post after a specific date?

    your example of ‘posts after today’ would ask for ‘scheduled’ or ‘future’ posts – this is a total different query, using the ‘post_status=future’ argument: http://codex.wordpress.org/Class_Reference/WP_Query#Type_.26_Status_Parameters

    for posts after a certain date in the past, you need to use a filter function;
    also from the Codex (only minor adjustment):

    // Create a new filtering function that will add our where clause to the query
    function filter_where( $where = '' ) {
    	// posts for March 1 to March 15, 2010
    	$where .= " AND post_date >= '2010-03-01';
    	return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' );
    $query = new WP_Query( $query_string );
    remove_filter( 'posts_where', 'filter_where' );
    Thread Starter LazySpidey

    (@lazyspidey)

    I found an easier way (for me)..

    I put this in the loop:

    <?php
    $postDateF = strtotime( $post->post_date );
    $todaysDateF = time() - (time() % 86400);
    if ( $postDateF >= $todaysDateF) : ?>

    By this i can check if a post is today or after today πŸ™‚

    Problem solved.. Thx a lot..

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Query_Posts and date function doesn't fork correctly’ is closed to new replies.