Limit RSS feed to previous calendar month

I want to filter my RSS feed to only show posts in the previous calendar month. I have this code in my theme functions file but it is not working?

// filter the RSS feeds to show only the last calendar month
$lastMonthNumber = date( 'n', current_time( 'timestamp' ) ) - 1; // work out the previous month number
function feedFilter($query) {
  if ($query-is_feed) {
    $query-set('date_query', array(
        array(
           'month' = $lastMonthNumber
           //'month' = 5
        )   
    ));
  }

  return $query;
}
add_filter('pre_get_posts','feedFilter');`

Topic date-query rss filters Wordpress

Category Web


This approach doesn't work for January:

$lastMonthNumber = date( 'n', current_time( 'timestamp' ) ) - 1; 

as it would give 1 - 1 = 0.

Here's another suggestion, using the string to time support of the date query:

$query->set( 'date_query', [ 
    [ 
        'after'     => 'midnight first day of last month', 
        'inclusive' => true,
    ], 
    [ 
        'before'    => 'midnight first day of this month', 
        'inclusive' => false, 
    ]  
] );

For example this should generate:

wp_posts.post_date >= '2018-05-01 00:00:00' AND wp_posts.post_date < '2018-06-01 00:00:00'

if the current day is 27th of June 2018.

Note that pre_get_posts is an action, not a filter.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.