Daily archive pagination with WP_Query in WordPress
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x


I'm looking for a clean way to utilize a daily pagination in a custom post type via WP_Query. I'm trying to avoid using database manupulation via wpdb. I'm trying to achieve a daily archive something like:

<- Tuesday | Wednesday | Thursday ->

Obviously you can't go forth to the day if it doesn't exist and if a day doesn't have any post it would paginate to the closest day with posts.

awarded to MSF

Crowdsource coding tasks.

1 Solution

To get the posts from a single week you can do:

$year = null;
$week = null;

if (is_null($year)) {
    $year = date('Y');

if (is_null($week)) {
    $week = date('W');

$query = new WP_Query('year=' . $year . '&w=' . $week);

The problem with this is that it's unclear what to do if no posts exist for that week.

Another option would be to broaden the range of days it queries for:

$query = new WP_Query('m=' . date('Ym')); // gets all the posts for the current month

This would make it less likely to return 0 results, but it would still be a possibility (specially in the early of the month). You could also add a callback filter to WP_Query to return all posts between two days (say between today - x days up to today + y days); that would solve the problem when the month has just started and no posts exists, but you would need to make a custom query and it would most probably be less efficient than using wpdb directly. Example from the WP Codex:

function filter_where($where = '') {
    return $where . " AND post_date >= '2013-07-01' AND post_date <= '2013-07-11'";

add_filter('posts_where', 'filter_where');
$query = new WP_Query('1=1');
remove_filter('posts_where', 'filter_where');

The ideal solution is a SQL query that would fetch all the distinct post dates and order those results by the absoute difference from today / your target date, but that would require wpdb.

Another alternative, that may be higly inefficient (depending on how many posts you would want to fetch in the first place) would be to pull a reasonable amount of posts from the database (enought to cover your pagination links), and group / order those directly in PHP.

Either way, I see this as highly ambiguous. Here is an hypotetical example of posts dates:

  • 2013-07-02 (Tuesday)
  • 2013-07-08 (Monday)
  • 2013-07-10 (Wednesday)

If you were to paginate those results, you would get:

<= Monday | Tuesday | Wednesday =>

The problem here is that Tuesday refers to the last week, even tho it appears after the Monday.

If you want to clear that up I'll be glad to help further.

Okay I see. I would prefer not to use wpdb. If I was to have a weekly loop, how would that look like? I would like to have it dynamic, and not with fixed dates. Possible? The markup would be something like: <= Previous week | Next week => Week 1 ... looping entries from the current week ... <= Previous week | Next week =>
cphilippsen 8 years ago
@cphilippsen: You mean paginate by previous / next week instead of weekdays? What if the chosen week doesn't have any posts to show? Would you want to skip it from the pagination or just show a "no posts found" message?
alixaxel 8 years ago
I'm still interested in the daily archive as well, but I think that a weekly archive will fit the needs better for this project. If there could be an option for skipping or showing a message that would give some flexibility.
cphilippsen 8 years ago
@cphilippsen: Now you totally lost me. With either days or weeks, you need to build the query as I have given some examples above (using dynamic values from $_GET if specified, today otherwise). Then you must check if posts exists for that date, or show a "no posts" message - skipping empty ranges would be way more inefficient or require you to access wpdb directly. Finally, for building the pagination links, you need to change their query parameters (be sure to convert the existing values to dates first, and then back again) to -1 and +1 respectively. That's the gist of it, or at least what I think you might want to do. Also, aside from being uncertain if this is what you want, I'm not exactly sure which part you need help with.
alixaxel 8 years ago