Date Pagination

A WordPress plugin to paginate your posts by year, month or day.

Download it here

This plugin provides an easy way to paginate posts by year, month or day in your theme’s template files. Use the native WordPress pagination functions to display the pagination links.

It’s likely your theme is already using these functions for normal pagination. If not, see if it works with your theme’s pagination or add them yourself.

To tell WordPress a page should be paginated by dates set the date_pagination_type query argument to 'yearly', 'monthly' or 'daily' for a custom query (WP_Query), or in the pre_get_posts action. (See the examples below)

After that the posts will be paginated by date!

Note This plugin intentionally doesn’t do pagination for date archives. See this plugin to do just that.

Note: It’s recommended you create a child theme whenever you edit your theme template files.

Using dates in the pagination links

The WordPress pagination functions have labels similar as “Previous Page” and “Next Page” for the pagination links. If you would like to display dates instead use the label functions provided by this plugin.

To have the WordPress numerical pagination functions display dates instead of page numbers use the numerical pagination functions provided by this plugin instead.

Examples

These examples show you how to add Date Pagination to pages by seting the date_pagination_type query var.

Date pagination with pre_get_posts

You can set the type of date pagination to 'yearly', 'monthly' or 'daily' with the 'date_pagination_type' query variable.

Conditional tags let you choose where you want to use date pagination.

In this example we set the pagination to monthly on the home page. Use this example in your (child) theme’s functions.php file.

add_action( 'pre_get_posts', 'monthly_paginated_home_query' );
function monthly_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() ) {

			// set the date pagination to 'monthly'
			$query->set('date_pagination_type', 'monthly'); 

			// set other arguments here
		}
	}
}

See this example loop for how you would use the WordPress pagination functions in your theme.

Date pagination with WP_Query

You can set the type of date pagination to 'yearly', 'monthly' or 'daily' with the 'date_pagination_type' argument.

In this example we set it to 'monthly'.

<?php
// Get the paged variable and use it in the custom query.
// (see: http://codex.wordpress.org/Pagination ).
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// Example arguments.
$args = array(
	// set the date pagination to monthly
	'date_pagination_type' => 'monthly', // 'yearly', 'monthly', 'daily'
	'paged'                => $paged,
);

// The custom query.
$the_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Your theme's loop code here -->
<?php endwhile; ?>

For a more comprehensive example of a custom query and the loop using the WordPress pagination functions see this example.

Troubleshooting Steps

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

Troubleshooting steps if you queried the loop with new WP_Query.
Did you set the paged parameter?
Is pagination broken on a static front page?
Did you reset the loops if there are multiple loops?
Did you add the $max_pages to the next_posts_link() function?
Did you add the total to the paginate_links() function?

48 thoughts on “Date Pagination

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

    1. 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?

  2. 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.

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

      1. 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. Hi keesiemeijer,
    Good job with your plugin!
    I am working on the blog section of this website
    http://artistresidencecornwall.co.uk/whats-on/
    to get the posts in monthly pagination.
    I am using this plugin http://wordpress.org/support/plugin/the-future-is-now to have the possibility to publish post/events coming in next months.
    Problem is that the first posts on the blog will be future dates.

    Is there a way to use your plugin in order to display posts from the current month, when you land on the blog page?
    I’d really appreciate your help,
    if something sounds confusing I will post the code in codepen or pastebin

    Thanks
    –Simon

  5. hello keesiemeijer,

    I’m trying to use this pagination with the option set as daily.

    using the code below though just gives me the current date for each link. They display the correct posts though, they just doesn’t display the correct date.

    thoughts?

    if ( function_exists( 'km_dp_get_next_date_label' ) ) {
        $next_label = km_dp_get_next_date_label( 'F j, Y' );
    }
     
    if ( function_exists( 'km_dp_get_previous_date_label' ) ) {
        $prev_label = km_dp_get_previous_date_label( 'F j, Y' );
    }
    
      1. Yes, it displays in pagination link. $now is only for compare dates. By the way, is it difficult to put an option ‘weekly’ to your plugin?

  6. Great plugin! This is exactly what I’m looking for, however I’m wondering if there was a way I could get each page to show 2 month of posts instead of just one month. A sort of bi-monthly pagination.
    Let me know if you think this is possible and how I could implement it.
    Thanks,

    1. Hi akramsideas

      That can be done with some changes to the plugin. What kind of labels do you want for the next and previous links? With dates in them or just plain “Next posts” “Previous posts”

  7. I’m ran into an issue with the plugin the minute I posted my first 2015 post. It caused the previous month to suddenly add images and revisions as posts in the loop. It was really weird.

    I’ve disabled the plugin at the moment, but was wondering if you might have an idea what could be causing the issue.

    Thanks.

      1. Hey
        This doesn’t work for me . basically i want the two month pagination showing posts of this month and next month on one page and future posts like Nov is going on January and Feb posts on next page. and say if December having no posts then it should show Nov and Jan posts on one page and Feb and march posts on next page pagination.

        Pls reply ASAP.

  8. I’ve got kind of an odd situation for you. My client is using this plugin to create archives by year, but they want the first year in the archive to be 2014, the next 2013, etc. I can do this by setting “order” to “DESC,” but then the posts themselves show up in descending order as well. Is there any way to have the pages go in descending order, but the posts in ascending? I.E. January would be first, February second, March third, but he pages would go 2014, 2013, 2012?

    1. My theory is to create an array, reverse the array, and output, but I’m having some trouble with that getting the old dates rather than the new. Will report back as I progress…

  9. Hi,
    WordPress database error: [Column ‘ID’ in field list is ambiguous]
    SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM ahf_posts JOIN ahf_icl_translations t ON ahf_posts.ID = t.element_id AND t.element_type = ‘post_event’ JOIN ahf_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND ahf_posts.post_type = ‘event’ AND (ahf_posts.post_status = ‘publish’ OR ahf_posts.post_status = ‘private’) AND t.language_code=’ur’ GROUP BY YEAR(post_date) ORDER BY ahf_posts.post_date DESC

    i am receiving this error.. i am using wpml plugin…

  10. Hi, I’m trying using your plugin. It works ok on the main language, but it does not show anything for the secondary language. I’m using wpml and your plugin by shortcode in the single post page (single.php) via do_shortcode function. What can I do? Thanks

  11. I love the Custom Post Type Date Archive, but is there a way for it to get along with this plugin, or for the CPT Date Archives to have daily pagination? Maybe Date Archives Pagination is for this purpose? It doesn’t seem to have much documentation so I didn’t know if it is a working plugin. I know this post is a little old, but the plugins are so perfect for what I’m working on. Thanks for any help you can offer!

  12. Hi Kessie,

    I found your plugin and it saves me lots of work.
    I’m doing monthly pagination and I wonder if there’s any way to show the page for the current month, instead of future month, for my event post (custom post type)?

    Thanks & Regards,
    Harold

  13. Hi Kessie, I have copy and pasted your “WP_Query loop with date pagination” example into a page template, but setting ‘date_pagination_type’ => ‘daily’ displays monthly instead of daily archives. Have I forgot something to make the ‘daily’ archives work? Thanks!

  14. Thank you very much for the plugin! I have been using it with the β€˜pre_get_postsβ€˜ action, and it works perfectly!

    But recently I try to use it with a custom query, it doesn’t work as expected. Here’s the code I used: https://www.bobz.co/filter-wordpress-posts-by-custom-taxonomy-term-with-ajax-and-pagination/, I used ‘date_pagination_type’ => ‘monthly’ in the “WP_Query” part (https://www.bobz.co/filter-wordpress-posts-by-custom-taxonomy-term-with-ajax-and-pagination/#step-3-2).

    could you help to see what could be wrong? I am thinking if it is the action hooks “wp_ajax_do_filter_posts” and “wp_ajax_nopriv_do_filter_posts” there causing the problem.

    Many many thanks in advance! ^^

  15. Hello Kessie, thanks your plugin works great, I have it installed in a client website and we all are very happy with it.
    The only request I received and I can’t accomplish is to list all the dates available and not just the one selected and the previous and next one.
    Is it possible? could you please give me a hint?

    Thanks !
    Pietro

        1. I’ve added the plugin to the WordPress repository as well.
          https://wordpress.org/plugins/date-pagination/

          There are also some new functions to list dates as in the WordPress numerical functions.
          https://keesiemeijer.wordpress.com/date-pagination/functions/#numerical-pagination

          To list all dates try it with this in your (child) theme’s template file

          <?php
          // get date pagination variables
          $type       = get_query_var( 'date_pagination_type' );
          $valid_type = km_dp_date_pagination_is_valid_type( $type );
          $dates      = get_query_var( 'date_pagination_dates' );
          
          // Check if dates are found on a date paginated page
          if ( $valid_type && !empty( $dates ) ) {
          
          	echo '<ul>';
          	for ( $i=0; $i < count( $dates ) ; $i++ ) {
          
          		// Create link with 'monthly' format.
          		$link = '<a href="' . get_pagenum_link( $i + 1 ) . '">';
          		$link .= mysql2date( 'F Y', $dates[ $i ] ) . '</a>';
          
          		echo '<li>' . $link . '</li>';
          	}
          	echo '</ul>';
          }
          ?>
          

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