Date Pagination

This plugin lets you paginate your posts by year, month or day.

This plugin is aimed at developers and provides an easy way to paginate posts by year, month or day in theme templates. It uses the native WordPress pagination functions for display of the pagination links.

Use the ‘date_pagination_type’ query argument with a custom query (WP_Query) or with the ‘pre_get_posts‘ action, and this plugin will do the rest. To display the date for the next, current and previous pages use the functions km_dp_get_next_date_label(), km_dp_get_current_date_label() and km_dp_get_previous_date_label().

Plugin support for this plugin from the plugin author will be minimal as this is a plugin made for developers. If you encounter problems see the troubleshooting steps first.

Bug reports, feature requests and other issues can be reported through the GitHub issues system.

Download

**Note** This plugin intentionally doesn’t work in date based archives.

Example pre_get_posts

Set the pagination to monthly on the home page. Use conditional tags if you want to use date pagination on other pages

Use this example in your (child) theme’s functions.php file.

add_action( 'pre_get_posts', 'yearly_paginated_home_query' );
function yearly_paginated_home_query( $query ) {

	// not a wp-admin page and the query is for the main query
	if ( !is_admin() && $query->is_main_query() ) {

		//  on the home page only
		if ( is_home() ) {

			// 'yearly', 'monthly', 'daily'
			$query->set('date_pagination_type', 'monthly'); 

			// set other parameters here
		}
	}
}

Example WP_Query

<?php
// example args
$args = array(
	// 'yearly', 'monthly', 'daily'
	'date_pagination_type' => 'yearly',
	'cat' => 25,
);

// the custom query
$the_query = new WP_Query( $args );
?>

<!-- the loop -->

Date Label Functions

To get the current date of a page you can use the km_dp_get_current_date_label() function. This function has the same arguments as the km_dp_get_next_date_label() and km_dp_get_previous_date_label() functions below

To get the date for the next and previous pages there are two functions. Use them if you want the date in the labels (e.g. Next Month: June 6, 2010) of the WordPress pagination functions. The functions are km_dp_get_next_date_label() and km_dp_get_previous_date_label(). Both functions have the same arguments.

Basic Usage

<php km_dp_get_next_date_label( $format, $query ); ?>
$format
(string)(optional) PHP date format
$query
(object)(optional) WP_Query object that is paginated by year, month or day.

Example pre_get_posts (normal loop)

Example with monthly pagination set with the pre_get_posts action.

<?php if ( have_posts() ) : ?>

<h2><?php echo km_dp_get_current_date_label(); ?></h2>

<?php while ( have_posts() ) : the_post(); ?>
<!-- rest of the loop -->
<?php endwhile; ?>
<?php
// default labels
$next_label = 'Previous Month';
$prev_label = 'Next Month';

// Check if functions exists (plugin is activated)
// format example: F Y - will become November 2010
if ( function_exists( 'km_dp_get_next_date_label' ) ) {
	$next_label = km_dp_get_next_date_label( 'F Y' );
}

if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
	$prev_label = km_dp_get_previous_date_label( 'F Y' );
}

// WordPress pagination functions (see the Codex)
next_posts_link( $next_label );
previous_posts_link( $prev_label );
?>
<?php else:  ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Example WP_Query loop

Example of a custom query paginated by year.

<?php
// set the paged variable (see: http://codex.wordpress.org/Pagination )
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// example args
$args = array(
	// 'yearly', 'monthly', 'daily'
	'date_pagination_type' => 'yearly',
	'cat' => 25,
	'paged' => $paged,
);

// the custom query
$the_query = new WP_Query( $args );
?>

<?php if ( $the_query->have_posts() ) : ?>

<h2><?php echo km_dp_get_current_date_label(); ?></h2>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- rest of the loop -->
<?php endwhile; ?>

<?php
// default labels
$next_label = 'Previous Year';
$prev_label = 'Next Year';

// The year in 4 digits with format 'Y', and the custom query object
if ( function_exists( 'km_dp_get_next_date_label' ) ) {
	$next_label = km_dp_get_next_date_label( 'Y', $the_query );
}

if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
	$prev_label = km_dp_get_previous_date_label( 'Y', $the_query );
}

// WordPress pagination functions (see the Codex)
next_posts_link( 'Previous year: ' . $next_label, $the_query->max_num_pages );
previous_posts_link( 'Next year: ' . $prev_label );
?>

<?php wp_reset_postdata(); ?>
<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Troubleshooting Steps

The same troubleshooting steps for WordPress pagination functions apply to this plugin.

Custom Query

These are the troubleshooting steps if you queried the loop with new WP_Query or query_posts.
Did you set the paged parameter?
Is pagination broken on a static front page?
Did you reset the loops if there are multiple loops?

Functions

Did you add the $max_pages to the next_posts_link() function?
Did you add the total to the paginate_links() function?

8 thoughts on “Date Pagination

  1. David

    Hi,

    I have been looking for this type of plugin for a long time ….. think the idea of using dates for pagination is the way to go, especially for sites that generate a lot of content.

    I am more a designer, with an appreciation of development … but this is a little bit past my skill set at the moment.
    Are there any plans to release an out of the box plugin? Would really love to get this running on the sites I work with.

    Many thanks in advance
    David

    Reply
    1. keesiemeijer Post author

      Hi David,

      Unfortunately this is as out of the box as it gets. It’s up to a theme where to display pagination, and what functions (or pagination plugins) are used. The plugin can’t know, filter or detect this. For the most part the plugin will be compatible with themes that use the native WordPress pagination functions.

      Where do you encounter problems? On what pages do you want date pagination?

      Reply
  2. catering trailer insurance

    Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

    Reply
  3. Jordan Lejuwaan

    Is there any way this plugin can be altered so that the posts within each day can be sorted by a meta value? If I add a normal WP ‘orderby’=>’meta_value_num’ into this current, it spits out the days in order of votes :/

    Reply
      1. Jordan Lejuwaan

        It seems that WordPress allows you to put two variables in the orderby parameter, first one being dominant, but if I just put ‘date’, then it orders by the timestamp, not by the day. So I will have to create another meta field that just stores the day of publishing instead of the timestamp. If you have a better solution, please let me know!

  4. Pingback: Default blog section in wordpress paginated monthly - HelpDesk

  5. nhl

    Unquestionably believe that which you said. Your favorite reason appeared to be on the internet the simplest thing to be aware of.
    I say to you, I certainly get irked while people think about
    worries that they just do not know about. You managed to hit
    the nail upon the top and also defined out the whole
    thing without having side effect , people could take a signal.

    Will probably be back to get more. Thanks

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s