If this is possible, how to get a RSS feed for the previous week's posts?

I found a sollution from @Bainternet (see 1, 2), but I don't understand how to implement it in my case.

Right now I can display posts from the previous week (see the bellow code, adapted a little from the wpbeginner.com), but I need a feed link also.

// Display previous week’s posts
add_shortcode('lastweek', 'wpb_last_week_posts');

function wpb_last_week_posts() {
    $thisweek = date('W');
    if ($thisweek != 1) :
        $lastweek = $thisweek - 1;   
    else : 
        $lastweek = 52;
    $year = date('Y');
    if ($lastweek != 52) :
        $year = date('Y');
        $year = date('Y') -1; 
    $the_query = new WP_Query( 'year=' . $year . '&w=' . $lastweek .  'posts_per_page=5' );
    if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><a href="<?php the_permalink(); ?>" title="Permanent link to <?php the_title(); ?> "><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    <?php else:  ?>
        <!-- Display recent posts if no posts in the previous week -->
        <ul><?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
            <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                <?php the_excerpt(); ?>
            <?php endwhile;

    <?php endif;
  $output = ob_get_contents();
  return $output;

You could try to modify the feed query directly via:

 * Modify the main feed query to show posts from the previous week, 
 * when the GET parameter wpse_previous_week is set
 * @link http://wordpress.stackexchange.com/a/194843/26350
add_action( 'pre_get_posts', function( $q )
    if(    $q->is_feed() 
        && filter_input( INPUT_GET, 'wpse_previous_week', FILTER_SANITIZE_NUMBER_INT ) 
        $q->set( 'date_query', 
                    'before'    => 'Sunday previous week', 
                    'after'     => 'Monday previous week',
                    'inclusive' => true 
} ); 

and view the feed with:


Note that you might have to test and modify the date strings to better suit your needs.

| improve this answer | |
  • You can modify the relative date string of date_query to your needs. It means you can use text to define the dates. I just updated the answer with another date string suggestion. The 1 only means that this will be actived, try 0 and it will not be actived. In this answer I try to reuse as much as possible, so we don't have to build things from scratch ;-) – birgire Jul 17 '15 at 21:25
  • Ok! I need to test this more. Right now your solution works and I am happy :). Thank you! – Iurie Malai Jul 17 '15 at 21:35
  • you're welcome, hopefully you can adjust this to your needs. – birgire Jul 17 '15 at 21:42
  • Is it possible to set the first day of the week in your function (this must not affect the rest of the WP install)? I remarked that right now it works as though the first day of the week is Sunday, even if I set it in the WP options to Monday, so I need more control to not depend of how the first day of week changes. I tested with date_default_timezone_set('Europe/Chisinau'); but this doesn't helped. Right now I changed 'before' to => 'Sunday this week'. – Iurie Malai Jul 19 '15 at 6:42
  • 1
    We are only modifying the feed query when actiated by our GET parameter, so this will not affect anything else. The date_query part of the WP_Query class is rather flexible, check out the codex here. The relative date strings comes in many flavours, see here. If you want to control the date_query from the backend (wp-admin) then you would have make the corresponding get_option() calls. You can check my answer here to get some testing ideas. @lurie – birgire Jul 19 '15 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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