First, get the current month and year:
$month = int(current_time('m'));
$year = int(current_time('Y'));
Next, get the posts in this month:
$query = new WP_Query( 'year=" . $year . "&monthnum=' . $month );
If the query returns empty query the month before and repeat this until you have found a non empty month:
while (empty($query)) {
$month = $month-1;
if ($month == 0) { $month = 12; $year=$year-1;}
$query = new WP_Query( 'year=" . $year . "&monthnum=' . $month );
}
Related Posts:
- Should we trust the post globals?
- Get first post from wp_query
- Accessing the post content with WP_Query
- Querying post from a multisite network
- How to query serialized array by comparing greater than (>=)?
- How to know if get_posts() failed?
- Show related posts by category but ignore one category
- post__in for get_posts with a dynamic array
- Query posts distinct authors
- How to get previous 10 days post from a specific date – WP Query
- Post content being duplicated by the_content();
- Specific loop in Shortcode
- Sort posts by activity date
- How to add padding between posts
- prevent showing posts of an specific category in admin posts section
- Working Bootstrap Carousel Conversion to WP – Technical Questions
- Search widget breaks when using multiple loops?
- How to change bulk post status
- Add few specific post ids to wp_query
- WP_Query posts_per_page ignored
- wp_query ‘s’ parameter does not work with custom post type
- WP Query related posts by current page Tag ID
- Custom WP_QUERY $args
- WP_Query get posts from custom posts by category
- Accessing post->ID outside of the loop for listing child pages
- Limiting the number of posts in WP_Query leads to unexpected result
- List posts in a given category
- Reset Popular post query?
- Query Posts multiple conditions
- Future post ID not showing
- Search by post title and content in wp_query without order
- WP Query – Posts Per Page not working in combination with category__in
- How to do set post permalinks using 6 digit random unique function?
- How safe is renovating $wp_query when doing WP_Query pagination in Page Template
- posts_per_page option limits the number of Gallery items
- wp_query random post
- wp_posts table: safely remove unused columns to save database storage
- Using wp_list_pages() after calling query_posts()
- how to get post order by post id wp_query?
- Possible to alternate between two loops
- How to display all posts divided/ordered by post date? [closed]
- Why are my paginated posts always returning the same results?
- Is it possible to use WP_Query to only pull posts with attachments?
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- Every second post different class in blog view
- Display posts of specific category term
- Query posts by Author and/or by Tag
- Can’t understand why sometimes a [caption] field appears
- Display current post position in Elementor Posts widget
- Why in my query is display two title?
- Migrate posts from category and sub-category via SQL
- wp_query with ajax
- Exclude some posts from displaying in wp_query based on some condition
- WP_Query custom field pass the post id
- How to convert query sql to shortcode in wordpress?
- Use posts_where to exclude posts ids from wp_query
- Why does WordPress search returns same number of results for every search query?
- Sidebar limiting to 10 posts?
- WP_Query get posts in custom database table [duplicate]
- WP query taxonomy optimization
- Override WordPress core post-template.php block template
- WP_Query outputs wrong post in custom post type
- Change Old WordPress Post Date Year
- Displaying all posts from other sites on the network on one site
- Display/Filter post using if else statement
- Search results for ACF data
- Multisite how to display merged posts from two sites and sort by latest date?
- Query prints posts without specific categories
- Numbered Pagination Showing The Same Posts After Altering WP_Query
- Order by meta_key field in WordPress not meta_value field value
- How to remove a post from results by ID after query?
- Using a meta_value or postdate to to query and orderby
- Exclude posts in home slider from sections
- Ajaxify Post Sort
- Loop through all posts showing duplicates
- WP_Query extended with AJAX
- Manipulate query to show specific post
- Get current user, change users post status to published
- WordPress text field in array problem
- Random order of the 5 most popular posts
- My custom query is displaying random posts in random order
- Get query result according to merged array
- Display WordPress Post By Date
- GUID to related post not in current language
- How can I display a specific number of post in a category via a url
- Advanced Post Display/Pagination/Ordering
- How to show the posts list into a static page? Problems to use the loop into a static page
- Displaying Results From Custom Taxonomy Query
- Make assigning post to a specific category equivalent to assigning it to all categories
- Multiple loops on a Search result page?
- WP_Query retrieve custom posts but not the post showing
- WP_Query: Fetch posts that are in (category1 and not in category2), OR posts that are not in cagegory1
- WP_Query based on a custom field result
- Exclude sub sub categories from displaying in the archive page
- How do I show the post title if an advanced custom field hasn’t been used?
- WP_query sort by custom meta_key “price”
- How add class the_content();?
- Add custom PHP (no-SQL) filter to WP_query
- Filters do not work when there are multiple (one works)
- date_query returning only the most recent post instead of the post published before today