scriptygoddess RSS Feed
 
 
 
 

Display all posts grouped by date

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. 1
    Carthik:

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

  2. 2
    Carthik:

    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. 3
    Jennifer:

    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. 4
    Carthik:

    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!

Bookmarks

WordPress Resources

Meta

Random Stuff