0

I'm trying to set up a frontpage with all the post from the current week. Well this was quiet "easy" (the query snippet):

$query_string = array(
    'post_type' => 'post',
    'date_query' => array(
        array(
            'year' => date( 'Y' ),
            'week' => date( 'W' ),
        ),
    ),
    'post_status' => 'publish'
);
query_posts($query_string);

this was modifying my loop to show just the current weeks posts. Well now the problem - this way the pagination will not work. As I filter the main query for the current week WP seems to think this is the whole result set.

So how to achieve my goal to display the selected weeks posts with a pagination to toggle "back in time"?

I think I'm totally wrong so far :(

Cheers Marcel

1 Answer 1

1

Use the page number to calculate a new date to get the week to fetch:

$page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$date_to_fetch = time() - ( 604800 * ( $page - 1 ) ) );
$query_string = array(
    'post_type' => 'post',
    'date_query' => array(
        array(
            'year' => date( 'o', $date_to_fetch ),
            'week' => date( 'W', $date_to_fetch ),
        ),
    ),
    'post_status' => 'publish'
);
query_posts($query_string);

If you are working with date( 'W' ), you should always be working with date( 'o' ) instead of date( 'Y' ), because at the beginning of a year it is possible that you are still in a week which is accounted to the past year. For this see the documentation of the date function: http://php.net/manual/de/function.date.php

3
  • yeah I know - the code I've posted is very simple to show you what I try to achieve. But even if I use your code there won't be any pager element to go back for one week because the resultset is still one week (so that WP can't calculate the pager elements)
    – Marcel
    Nov 25, 2014 at 20:48
  • Yes, the normal functions for paging won't generate the elements. You will have to write your own function and add it to the template. Other options are: - Hook into wp_link_pages filter, which is used by the wp_link_pages() function. - Try modifying the globals $multipage and $numpages while in the loop. See the source of wp_link_pages(): core.trac.wordpress.org/browser/tags/4.0.1/src/wp-includes/…
    – JHoffmann
    Nov 25, 2014 at 21:35
  • hmmm damn it. this is the way I didn't want to go... thank's for the hints!
    – Marcel
    Nov 26, 2014 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.