[wp-hackers] Hacking Assignment: Paging by Days

Sebastian Herp newsletter at scytheman.net
Mon Sep 6 13:16:50 UTC 2004


You realize that this is sloooooowwwww as hell?

First there is one query that retrieves ALL postings just to get the 
post_date of the newest one. And then you have a while-loop within a 
for-loop and you query the database EVERYTIME inside the while-loop just 
to get the date of the last posting ... and you use string-compare for 
date values ....

A solution that might work (untested, there should be a test if the 
queries returned zero rows):
....

	} else if (empty($q['nopaging']) && ! is_single()) {
	  $page = $q['paged'];
	  if (empty($page)) {
	    $page = 1;
	  }

	  if (($q['what_to_show'] == 'posts')) {
	    $pgstrt = '';
	    $pgstrt = (intval($page) -1) * $q['posts_per_page'] . ', ';
	    $limits = 'LIMIT '.$pgstrt.$q['posts_per_page'];
	  } elseif ($q['what_to_show'] == 'days') {
            $startrow = $q['posts_per_page'] * (intval($page)-1);
            $startdate = $wpdb->get_var("SELECT max(post_date) FROM $wpdb->posts
                        GROUP BY year(post_date), month(post_date), dayofmonth(post_date)
                        ORDER BY post_date DESC LIMIT $startrow,1");
            $endrow = $startrow + $q['posts_per_page'] - 1;
            $enddate = $wpdb->get_var("SELECT min(post_date) FROM $wpdb->posts
                        GROUP BY year(post_date), month(post_date), dayofmonth(post_date)
                        ORDER BY post_date DESC LIMIT $endrow,1");    
	    if ($page > 1) {
	      $where .= " AND post_date > '$enddate' AND post_date < '$startdate'";
	    } else {
	      $where .= " AND post_date > '$enddate'";
	    }

....
	  }

David House wrote:

>Try this one out for size. It's not very optimised, rips a lot of code out from
>ScriptyGoddess' plugin (so we'd need to check with her before making this core)
>and I haven't tested it thoroughly, but anyway, here's my code:
>
>http://xmouse.ithium.net/wp-content/code-snippets/other-stuff/page-by-days.phps
>
>
>  
>




More information about the hackers mailing list