scriptygoddess

21 Jun, 2004

Display all posts grouped by date

Posted by: Jennifer In: WordPress my-hacks additions

This function will list all posts, and group them by day. If you want to use it – add it to your my-hacks.php file. (See more information on how to use my-hacks.php here – link via Carthik)

Again – I made this function specifically for a personal use – so there's not a lot of 'easy customization' without specifically editing the code, so I apologize for the lack of ease-of-use – but I wanted to post this for my own "code storage".

Here's the function:

function displayAllPostsGrouped () {
global $wpdb, $tableposts;
$now = current_time('mysql');
$previousPostDate = ";
$firstrun = true;
$arcresults = $wpdb->get_results("SELECT ID, post_date, post_title FROM $tableposts WHERE post_date < '$now' AND post_status = 'publish' ORDER BY post_date DESC");
if ($arcresults) {
foreach ($arcresults as $arcresult) {
if ($arcresult->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($arcresult->ID);
$arc_title = stripslashes($arcresult->post_title);
if ($arc_title) {
$text = strip_tags($arc_title);
} else {
$text = $arcresult->ID;
}
$post_date = substr($arcresult->post_date, 0, 10);
$post_day = substr($arcresult->post_date, 8, 2);
$post_year = substr($arcresult->post_date, 0, 4);
$post_month = substr($arcresult->post_date, 5, 2);
if ($previousPostDate != $post_date) {
if (!$firstrun) {
echo "</p>";
}
echo "<p><b>".$post_month.".".$post_day.".".$post_year."</b><br />";
}
echo "<a href='".$url."'>".$text."</a><br />";
$previousPostDate = $post_date;
}
}
}
}

Post last updated: 6/22/04 9:18pm

4 Responses to "Display all posts grouped by date"

1 | Carthik

June 22nd, 2004 at 11:15 am

Avatar

Isn't that what index.php does, list all posts by date, that is?
What could this be used for?

2 | Carthik

June 22nd, 2004 at 11:18 am

Avatar

You could add one-off functions that are useful to the my-hacks.php file (creating one if there is none first, with a <?php two lines and then ?>) and then turning on my-hacks.php support in Options->Miscellaneous

The my-hacks.php is what was used to extend wordpress' capabilities before there were plugins, and even now, a lot of the hacks/modifications use this file.

3 | Jennifer

June 22nd, 2004 at 12:26 pm

Avatar

Carthik – I guess so. But wouldn't that only display the last x posts (as indicated in the posts per page value)? I wanted an *archive* page that would show ALL posts (ever written for the blog).

There are a few "archive listing" style functions – but none that let me format the text (and group by date) like I wanted it to… (at least not that I could find)

So what this function does is display an archive page of all posts like this:

03.21.2004
Title one here
Title two here
Another title

03.20.2004
Another title here
yet another title goes here

etc. etc…

4 | Carthik

June 22nd, 2004 at 4:23 pm

Avatar

I see. I used to use LaughinLizard's narchives.php which are sortable archives, that among the others, includes a date wise archives of all posts.

In the meanwhile, I found this guide to my-hacks.php which might serve as a repository for your one-off custom functions.

Cheeri-O!

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements