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.
Related Posts:
- How to show page content in feed?
- WordPress RSS feed – filter RSS content by custom field value
- Change WordPress RSS link with filter?
- Feed, RSS not able to clear the cache and cannot change the limit of cache life time
- How can I exclude tags from category_rss function?
- Change custom post type GUID in RSS
- How to add filter in custom rss feed
- Print only parent categories of post in custom RSS feed
- How to filter users on admin users page by custom meta field?
- Modify WordPress Rest Api Request/Response
- How to add some custom HTML into wordpress admin bar?
- Custom theme hooks / filters – passing arguments
- What params are available with the_content filter?
- How to hook wp_list_pages?
- Hook into admin post list page
- Add post/page ID to inserted links within the_content
- How to exclude/filter a tag from get_the_tag_list()
- Ajax, filters and shortcodes
- Filter the blog’s title without using global variables
- Multiple Ajax Data Action
- user_has_cap filter allows edit but will not allow save
- Add filter to wp_dropdown_pages() or wp_dropdown_categories() – no select container?
- Hook different functions to the same filter conditionally OR Pass additional arguments to existing filter?
- Changing a WordPress core function without hacking core
- Add Content Exclusively to a Custom Post Type Feed
- How to remove get_post_metadata using remove_filter inside a class?
- where to apply “apply filters” and other Sanitization Functions
- Add guid filter to attachment in media library grid mode
- Pass parameter to the upload_dir filter
- Sorting and limitation with pre_get_posts
- Warning: Missing argument 2 for widget_title filter
- Change term name only on front
- Adding Filter to Homepage only
- the_excerpt filter with an empty excerpt
- Add Defer Attributes to WordPress Scripts with consistent versioning
- Does auto_update_plugin Filter Work When Put In Theme’s functions.php File
- Remove CSS & JS files from WordPress Main Page For Increase Pagespeed?
- String regex match replace for role ‘contributor’ only
- In need of a content replace filter for posts in a specific wordpress category
- Can plugins that filter post contents work on posts from Word 2007?
- Inline Styles on all native blocks
- How to filter post content and force every link () made in blocks to return urldecode() with readable value?
- Better extend a class or use add filter/action hooks?
- Changing WordPress core without hacking core
- Link to Shop-Filter with .current-menu-item
- Adding a Clone link to product attributes – post_row_actions() filter, or how to override a Class question
- wp_upload_bits Upload Specific Sizes Only
- Remove posts inside pre_get_posts using a custom query
- Remove / Hide Attachment Display Settings in Add Media popup / dialog
- how to customize rss feed tags using hooks?
- Woocommerce “added to cart” message on main(home) page
- Why does adding a filter to ‘the_title’ break the server?
- Is it possible to apply filter to meta key value when querying posts?
- Add filter for specific post type only
- apply_filters with multiple args and multiple add_filter
- Avoid user registrations with specific email provider
- Should I remove_filter in order to replace a filter?
- How can I specify the post status of an untrashed post?
- filter hook to load a different post/page on current post/page
- Filter for author list in gutenberg core editor
- How would I be able to use a font awesome icon as a user’s avatar?
- Rewrite rule and display of post
- add_filter the_content doesn’t work
- Redirect to woocommerce checkout after adding to cart – item already in cart
- Passing variable from child theme function to parent theme filter
- `authenticate` filter never gets called
- paginate_links() Change the order of links
- Filter / add_action to upgrade.php page
- Add Filter – Pass Variable (PHP < 5.3)
- How to disable a wp filter in a certain admin panel page
- Is there a filter to programmatically change the HTML tab switching output of the page editor?
- Why does admin_body_class not work?
- post_orderby filter breaks function
- Disable Remember Me in Login Form
- Problem width wp_insert_post_data and Gutenberg block editor
- Counting a WP_Post Object value in an arary, using a filter?
- How to track a particular page in order to address the loading speed issue
- List all categories that have results in a query
- Distinguish between page and post in function
- How to filter page title for certain page?
- how to overwrite next_post_link
- Displaying posts on a page that have been assigned a value in a database
- Replace Text with hyperlinks
- adding an action inside if condition not working
- Adding filter to an specific page/site direction
- Print url to default featured image
- How to filter wp_get_recent_posts() to only posts that have thumbnails?
- Adding user filter – Not updating data in URL
- How to check which submission button was clicked?
- check, if any “add_action” function contains string XXXXXXXXX
- filter wptexturize doesn’t work on old posts titles
- remove_action conditionally for Custom Post Type – not working
- wp_get_attachment_link filter not working
- How would I remove an inline googleAPI font script in the the parent theme header.php?
- Getting entry ID from frm_email_message filter in formidable
- Is there a hook or filter that adds a button to the left of the search box?
- Filter and manipulate the get_current_user() function
- add_filter(‘the_content’, ‘method’) does not trigger my custom method
- Filter a custom post type by custom field in admin
- Search and Filter Using Custom Post Type, Custom Taxonomy, and Advanced Custom Fields