Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multiple loop queries for today/future/past posts WordPress

  • SOLVED

I have an events listing website where all posts are events that are posted to the event's date.

I am trying to piece together the code for my index page to display three sections:
— today's events, i.e. all posts dated today in descending order
— upcoming events, i.e. next 10 posts dated after today in ascending order (already set to "published" using Future is Now plugin, see here: http://bit.ly/sCnXb)
— past events, i.e. 10 posts dated before today in descending order

The posts dated before today section should be considered the "main" loop and have the pagination.

Can someone figure out the exact code to replace the standard loop? You can just leave a note where the post display code goes since I am able to do that.

NOTE (10/25/10 11:30pm): added notes about ascending/descending order, please note. The result should output like this in terms of order:

<blockquote>TODAY'S EVENTS
— Event, 1pm
— Event, 5pm
— Event, 7pm

UPCOMING EVENTS
— Event, tomorrow
— Event, 2 days from now
— Event, 3 days from now
(and so on x10...)

PAST EVENTS
— Event, yesterday
— Event, 2 days ago
— Event, 3 days ago
(and so on...x10)
</blockquote>

NOTE (10/26/10 12:21am): If it is easier and anyone is starting from scratch, the "upcoming events" could query all posts for next 7 days instead of just the next 10 posts.

Answers (2)

2010-10-26

Denzel Chia answers:

Hi,

Here are the codes for the loops.

Returns all post for the current date.

<?php
//returns post for just the current date
$today = getdate();
query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
while(have_posts()) { the_post();
//put your loop here.
the_title();
echo '<br/>';
the_content();
}
wp_reset_query();
?>


Shows 10 feature post

<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//feature post
$where .= " AND post_date > '" . date('Y-m-d') . "'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts('showposts=10');
while(have_posts()) { the_post();
the_title();
echo '<br/>';
the_content();
}
wp_reset_query();
?>


Returns 10 older posts

<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//feature post
$where .= " AND post_date < '" . date('Y-m-d') . "'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts('showposts=10');
while(have_posts()) { the_post();
the_title();
echo '<br/>';
the_content();
//navigation
if ( $wp_query->max_num_pages > 1 ) :
echo '<br/>';
next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'ipadwp' ) );
previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'ipadwp' ) );
endif;
}
wp_reset_query();
?>



Thanks


Katherine W comments:

Getting the error "Fatal error: Cannot redeclare filter_where()."

Not sure if this is an issue with your code, but all three loops need to be able to exist on the same page, here is what I have found on that topic:

http://codex.wordpress.org/The_Loop#Multiple_Loops


Denzel Chia comments:

Sorry, I used the function filter_where() twice,
rename the latest block of codes,


<?php

//Create a new filtering function that will add our where clause to the query

function filter_where_twice($where = '') {

//feature post

$where .= " AND post_date < '" . date('Y-m-d') . "'";

return $where;

}

// Register the filtering function

add_filter('posts_where', 'filter_where_twice');

// Perform the query, the filter will be applied automatically

query_posts('showposts=10');

while(have_posts()) { the_post();

the_title();

echo '<br/>';

the_content();

//navigation

if ( $wp_query->max_num_pages > 1 ) :

echo '<br/>';

next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'ipadwp' ) );

previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'ipadwp' ) );

endif;

}

wp_reset_query();

?>


Thanks


Denzel Chia comments:

I mean change the filter_where() to filter_where_twice for returns 10 older posts


<?php

//Create a new filtering function that will add our where clause to the query

function filter_where_twice($where = '') {

//feature post

$where .= " AND post_date < '" . date('Y-m-d') . "'";

return $where;

}

// Register the filtering function

add_filter('posts_where', 'filter_where_twice');

// Perform the query, the filter will be applied automatically

query_posts('showposts=10');

while(have_posts()) { the_post();

the_title();

echo '<br/>';

the_content();

//navigation

if ( $wp_query->max_num_pages > 1 ) :

echo '<br/>';

next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'ipadwp' ) );

previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'ipadwp' ) );

endif;

}

wp_reset_query();

?>


Katherine W comments:

Just tried the updated code, not getting an error but nothing is displayed at all for that block.

Also, I added further notes about the "future event" post query. The way you have it coded, the events furthest into the future are shown, but I need the next 10 that are happening to be shown in the order that they occur. Does that make sense?


Katherine W comments:

Follow up to the third block, I had tried the first update and nothing displayed. Just tried the second, no posts are displayed, only: echo '
'; next_posts_link( __( '← Older posts', 'ipadwp' ) ); previous_posts_link( __( 'Newer posts →', 'ipadwp' ) ); endif; } wp_reset_query(); ?>


Denzel Chia comments:

Hi,

The Future is Now plugin is causing the post content not to show at all, I had to drop my database and reinstall wordpress on my local host to fix it.
The plugin page says it compatible up to WordPress 2.6 only!

I am rewriting all the customs loops on my localhost now to test if it works now.

Thanks.


Katherine W comments:

Hi Denzel, no problem. Going offline for the night will check back in tomorrow morning.

I haven't had problems with TFIN plugin before, but it's the best solution I've found so far. I know there are some other plugins that do the same thing, if you get it to work with another similar plugin I can always switch.


Denzel Chia comments:

Hi,

I could not get date thing going, but managed to get all the custom loops working,
as I said before, the plugin had caused my wordpress database to malfunction, you may need to reinstall with your test data.

I had tested the following codes in my localhost,


<h2>Today's Events</h2>
<br />
<?php
$today = date('Y-m-d')." 0:00:00";
$tomorrow = date('Y-m-d')." 23:59:59";
global $wpdb;
$fivesdrafts = $wpdb->get_results("SELECT * FROM $wpdb->posts
WHERE post_status = 'publish' AND post_date BETWEEN \"$today\" AND \"$tomorrow\" ORDER BY post_date ASC");

foreach ($fivesdrafts as $post) {

setup_postdata($post);
//below here use template functions.
echo'<h3>';
the_title();
echo '</h3>';
the_content();
the_time();
echo '<br/>';

}
?>

<h2>Upcoming Events</h2>
<br />
<?php
//tommorrow to seven days later.
$tomorrow = date('Y-m-d', strtotime('1 days'))." 0:00:00";
$seven_days = date('Y-m-d', strtotime('7 days'))." 23:59:59";
global $wpdb;
$fivesdrafts = $wpdb->get_results("SELECT * FROM $wpdb->posts
WHERE post_status = 'future' AND post_date BETWEEN \"$tomorrow\" AND \"$seven_days\" ORDER BY post_date ASC");

foreach ($fivesdrafts as $post) {
setup_postdata($post);
//below here use template functions.
echo'<h3>';
the_title();
echo '</h3>';
the_content();
}
?>

<h2>Past Events</h2>
<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date < '" . date('Y-m-d') . "'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=10'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h4><?php the_title();?></h4>
<br/>
<?php the_content();?>
<?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago'; ?>
<?php endwhile; ?>
</ul>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>
<?php $wp_query = null; $wp_query = $temp;?>



Thanks.


Denzel Chia comments:

Forgotten to tell you that my latest codes does not need the future is now plugin, or any other plugins.

Thanks.


Katherine W comments:

This works, thank you so much!

2010-10-26

Andrzej answers:


Andrzej comments:


// --------- today - start ------------
$today = getdate();
query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
// display code start
if (have_posts()) : while (have_posts()) : the_post();
?><div <?php post_class() ?>><?php
if ( ! is_single() ) { ?><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2><?php }
?><small><?php the_time('F jS, Y') ?></small><?php
the_content('Read more &raquo; ');
edit_post_link('Edit', '<p>', '</p>');
?></div><?php
endwhile;
next_posts_link('Next page &raquo;');
previous_posts_link('&laquo; Previous page');
endif;
// display code end
wp_reset_query();
// ---------- today - end ------------------


// --------- after - start ------------
function filter_where_after($where = '') {
$where .= " AND post_date > '" . date('Y-m-d') . "'";
return $where;
}
add_filter('posts_where', 'filter_where_after');
query_posts('posts_per_page=10');
// display code start
if (have_posts()) : while (have_posts()) : the_post();
?><div <?php post_class() ?>><?php
if ( ! is_single() ) { ?><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2><?php }
?><small><?php the_time('F jS, Y') ?></small><?php
the_content('Read more &raquo; ');
edit_post_link('Edit', '<p>', '</p>');
?></div><?php
endwhile;
next_posts_link('Next page &raquo;');
previous_posts_link('&laquo; Previous page');
endif;
// display code end
remove_filter('posts_where', 'filter_where_after');
wp_reset_query();
// ---------- after - end ------------------


// --------- before - start ------------
function filter_where_before($where = '') {
$where .= " AND post_date < '" . date('Y-m-d') . "'";
return $where;
}
add_filter('posts_where', 'filter_where_before');
query_posts('posts_per_page=10');
// display code start
if (have_posts()) : while (have_posts()) : the_post();
?><div <?php post_class() ?>><?php
if ( ! is_single() ) { ?><h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2><?php }
?><small><?php the_time('F jS, Y') ?></small><?php
the_content('Read more &raquo; ');
edit_post_link('Edit', '<p>', '</p>');
?></div><?php
endwhile;
next_posts_link('Next page &raquo;');
previous_posts_link('&laquo; Previous page');
endif;
// display code end
remove_filter('posts_where', 'filter_where_before');
wp_reset_query();
// ---------- before - end ------------------


Katherine W comments:

Getting the error "Parse error: syntax error, unexpected T_ENDWHILE."

If I understand correctly, the pagination (next page/previous page) can only exist once per page, so it should only function on the "past"/"before today" query.