A plugin for adding the post date to wp_get_archives

The WordPress function wp_get_archives(‘type=postbypost’) displays a lovely list of posts, but won’t show the date of each post. This plugin adds each post’s date to those ‘postbypost’ lists, like so:

Add dates to wp_get_archives

Add dates to wp_get_archives

Usage

  1. Upload and activate the plugin
  2. Edit your theme, replacing wp_get_archives('type=postbypost') with if (function_exists('ard_get_archives')) ard_get_archives();

The function ard_get_archives(); replaces wp_get_archives('type=postbypost'), meaning you don’t need to specify type=postbypost. You can use all of the wp_get_archives() parameters except ‘type’ and ‘show_post_count’ (limit, format, before, and after). In addition, there’s a new parameter: show_post_date, that you can use to hide the date, but the plugin will show the date by default.

show_post_date
(boolean) Display date of posts in an archive (1 – true) or do not (0 – false). For use with ard_get_archives(). Defaults to 1 (true).

Customizing the date

By default, the plugin displays the date as “(MM/DD/YYYY)”, but you can change this to use any standard PHP date characters by editing the plugin at the line:

$arc_date = date('m/d/Y', strtotime($arcresult->post_date));  // new

The date is wrapped in tags, so you can style the date independently of the link.

How does it work?

The plugin replaces the ‘postbypost’ part of the function wp_get_archives, and adds the date to $before. The relevant code is below. You can compare it to the corresponding lines in general-template.php.

	} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
		('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
		$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit");
		if ( $arcresults ) {
			$beforebefore = $before;  // new
			foreach ( $arcresults as $arcresult ) {
				if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
					$url  = get_permalink($arcresult);
					$arc_title = $arcresult->post_title;
					$arc_date = date('m/d/Y', strtotime($arcresult->post_date));  // new
					if ( $show_post_date )  // new
						$before = $beforebefore . '<span class="recentdate">' . $arc_date . '</span>';  // new
					if ( $arc_title )
						$text = strip_tags(apply_filters('the_title', $arc_title));
					else
						$text = $arcresult->ID;
					echo get_archives_link($url, $text, $format, $before, $after);
				}
			}
		}
	}

The lines ending in ‘// new’ are the only changes.

So you want the date to appear after the title? Edit the plugin to modify $after, instead:

	} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) {
		('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC ";
		$arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit");
		if ( $arcresults ) {
			$afterafter = $after;  // new
			foreach ( $arcresults as $arcresult ) {
				if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
					$url  = get_permalink($arcresult);
					$arc_title = $arcresult->post_title;
					$arc_date = date('j F Y', strtotime($arcresult->post_date));  // new
					if ( $show_post_date )  // new
						$after = '&nbsp;(' . $arc_date . ')' . $afterafter;  // new
					if ( $arc_title )
						$text = strip_tags(apply_filters('the_title', $arc_title));
					else
						$text = $arcresult->ID;
					echo get_archives_link($url, $text, $format, $before, $after);
				}
			}
		}
	}

Download

Get the files here: (Current version: 0.1 beta)

Download the Ardamis DateMe WordPress Plugin

26 thoughts on “A plugin for adding the post date to wp_get_archives

  1. Iris

    Nice plugin! One question..is it possible to exclude certain authors off the archive list? Can’t figure that out, and I want to hide certain posts from appearing on the archive page.
    I did the trick on ‘recent_posts’ but I don’t want them showing up in the archives then either. Is there someone who can help me out?
    Thanks!

  2. Pete Roome

    Sorry my bad. Should have looked a bit harder before asking that question…for anyone else, there is some defaults at the top of the plugins php code. Cheers again this plugin is very handy.

  3. Pingback: WordPress Plugin: recent posts grouped by date | favrik. Waking up… » Blog Archive

  4. Jo

    Hey, thanks for that Plugin.

    I modified it a bit so that you can set for example German month names if you set for example “d. F Y” as the date format.

    Should I post you the code?

  5. sweet

    How can I make the date appear in my tab of recent posts? I replaced the wp_get_archives(); in the header and the footer. It works but when i try to replace it in the sidebar it does not work. Can you help me with this matter? Thanks!

  6. lotech

    Thanks – any plans to extend this to do a posts category too?
    Want to make a menu like so from the last 5 posts –
    Category Post Name
    Category2 Post Name2
    etc…

  7. ardamis Post author

    Getting the number of comments would require a separate query to the database. This plugin merely changes the behavior of the wp_get_archives function to report data already available to the function.

    Not that it couldn’t be done, but I’m not going to do it right now. Maybe someone will pick up on this and add that functionality, though.

  8. Pickle

    Hi this is just the archives styling I was looking for, but when I set it up there is no space between the date and the post title. Does anyone know if this is something I can fix by editing the plugin? Or my stylesheet perhaps? Thanks for the plugin and the help.

  9. Pickle

    Never mind, I see that I can add a space right after the date format, like this:
    $arc_date = date(‘m/y ‘, strtotime($arcresult->post_date)); // new

  10. Tom

    I second number 21’s question…

    Instead of adding the post date to the wp_get_archives list, how can I add the author name for that particular archived post instead? So, author name: post title.

    e.g. Mick Brown: My First Post

    Any help would be much appreciated!

  11. Leho Kraav

    here’s a simple patch to get configurable dates:

    Index: ardamis-dateme.php
    ===================================================================
    --- ardamis-dateme.php (revision 61)
    +++ ardamis-dateme.php (working copy)
    @@ -33,7 +33,7 @@
    else
    parse_str($args, $r);

    - $defaults = array('type' => 'postbypost', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'show_post_date' => true);
    + $defaults = array('type' => 'postbypost', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'show_post_date' => true, 'ard_date_format' => 'm/d/Y');
    $r = array_merge($defaults, $r);
    extract($r);

    @@ -80,7 +80,7 @@
    if ( $arcresult->post_date != '0000-00-00 00:00:00' ) {
    $url = get_permalink($arcresult);
    $arc_title = $arcresult->post_title;
    - $arc_date = date('m/d/Y', strtotime($arcresult->post_date)); // new
    + $arc_date = date($ard_date_format, strtotime($arcresult->post_date)); // new
    if ( $show_post_date ) // new
    $before = $beforebefore . '' . $arc_date . ''; // new
    if ( $arc_title )
    @@ -94,4 +94,4 @@
    }
    }

    -?>
    No newline at end of file
    +?>

    i should also note that this comment timeout system on ranks very high on the annoying list.

Comments are closed.