[wp-hackers] Too Many Queries

Mark Jaquith mark.wordpress at txfx.net
Wed Dec 6 08:53:04 GMT 2006


On Dec 5, 2006, at 8:30 PM, Andy Mikester wrote:

> I know month_num won't work but is there any way of getting the  
> month number
> so I can pass it in the PHP if statement? That way I only have to  
> run one
> query.

I'd do something like this:

<?php
function in_between_months($new_month) {
	echo '<hr />';
	echo '<h2>' . $new_month . '</h2>';
	return $new_month;
}

// query all your posts here
query_posts('cat=2&showposts=99999'); // 99999 = 'all posts',  
essentially...

$last_month = 0;
while ( have_posts() ) : the_post();
	$this_month = the_time('Y-m');
	if ( $last_month != $this_month ) {
		$last_month = in_between_months($this_month);
	}
	the_title();
	echo '<br />';
endwhile;
?>

So basically, you keep track of the year/month of the last post  
shown, and monitor for changes, executing in_between_months() when it  
changes.

--
Mark Jaquith
http://txfx.net/




More information about the wp-hackers mailing list