I think you should make a recursion function that will call itself if there’s no posts found passing new date to query for..
function getDatePosts( $date, $query_args ) {
$query_args['date_query'] = array(
'year' => date( 'Y', $date ),
'mounth' => date( 'm', $date ),
'day' => date( 'd', $date )
);
$query = new WP_Query( $query_args );
if( ! $query->have_posts() ) {
$date = $date - ( 60 * 60 * 24 );
$query = getDatePosts( $date, $query_args );
}
return $query;
}
And use it in your template:
$query = getDatePosts( time(), array( 'post_type' => 'post', 'posts_per_page' => 5 ) );
while( $query->have_posts() ) : $query->the_post();
// output
endwhile;
Not tested, but should work.
Related Posts:
- Query posts from current year
- How can I display a specific user’s first published post?
- Need to display a Jan 1st post as the site’s front page on Jan 1st, and Jan 2nd post as front page on Jan 2nd etc
- How to limit the number of posts that WP_Query gets?
- Should we trust the post globals?
- How to get posts published between a date and today?
- How to get post creation date?
- Changing the post date and time with function
- Random sort within an already sorted query
- Is it possible to Schedule Attachments in WordPress?
- how to get a different html for odd/even posts?
- using wp_update_post on save_post
- How to put last edited entry on top of the blog-post list?
- How to know if get_posts() failed?
- Using $wpdb to query posts with meta value containing current post_id
- WordPress Number of Posts Not Changing With posts_per_page
- Update existing post dates to random dates
- I have over 4000 posts, will querying some of them cause performance issues?
- Duplicate posts
- Looping through posts per category gives same posts for each category
- How to show posts rank based on custom field value
- featuring old articles without messing up with the archive
- How do I create Comma Separated list of attached image ids?
- How to add posts to wp_query result?
- Trying to put an array into ‘post__in’ => array() query not working
- How to display an icon when a new post is published and then remove it when a specific time past?
- How to output comments number of a post per day?
- Human Time Diff, change mins to minutes
- Order post by year DESC and month ASC
- Display custom post types by date field
- Should ‘setup_postdata()’ be reset with ‘wp_reset_postdata()’?
- Query All users that has post
- wp_enqueue_media() slows down my WP site
- Can wordpress differentiate between added and updated posts?
- Fetch posts from current week (Sunday to Saturday)
- How can I get all posts data from within a paginated search result?
- Search query – exact post title match
- Sorting posts alphabetical that have single digits
- Query posts from different categories in multisite
- How to set post expiration date and time and move the page to archive after expiration [closed]
- Check if post exists
- posts_per_page doesnt work
- Sorting posts according to view counts not working
- Display page content AFTER a loop of posts
- Only display posts after current date
- Get posts by multiple ID’s (query)
- How to start with post number x?
- Exclude posts with empty post_content in wp_query
- Only display a certain number of posts, dependent on how many posts there are available in a query
- Improving WP_Query for performance when random posts are ordered
- Display post from specific date
- Should $found_posts be returned as string in ‘found_posts’ filter hook?
- Get random posts between specific dates / of specific age
- XML-RPC and post_date
- Automatically republish old posts
- Query how many items to show in shortcode
- Replace ‘published on date’ with ‘modified on date’ on Posts
- pre_get_posts query between 2 dates (date stored in custom post meta)
- Get posts by name and taxonomy term
- WP_Query Authors OR Categories
- Get Posts that are in the current month or later
- How can I show many posts an author has per week?
- Sticky post appears twice
- do_shortcode within post query
- If modified on same day, show only time
- How Can I Query a Specific Page From a MultiPage paginated Post
- Get Meta Key Value While Saving Post
- Most liked page not displaying posts
- Query get post,how to add comment box
- Update all comments time to random dates?
- filter RSS feed in URL
- WordPress custom Query for Posts in Category display posts multiple times
- get query() without post content?
- WordPress Sidebar menu for posts based on date – guidance needed
- How can I setup a relationship using categories in WordPress?
- Exclude the first ‘n’ number of posts of a tag from home page?
- date issue with category post retrival
- posts_per_page option limits the number of Gallery items
- How i can limit period of post publication?
- How to setup a query to output posts by groups of five?
- How to get date of post when using wp_get_recent_posts()?
- Worpdress function for difference dates
- Exclude current custom post on single post
- Dynamically switch template on click
- How to get posts and comments amount per hour, per year and per month?
- Get all custom post types excepted some…
- Get all comments of author’s posts
- First x post with another template then the others
- Why Query is returning empty array?
- Delete all drafts?
- Wrap every month posts in div
- Inner join overrides Advanced Custom Fields plugin’s get_field [closed]
- meta_value timestamp older than now
- Counter is skipping post when I still want it visible [closed]
- Changing default WP-Site creation date
- Do not show children of a category
- post__in not working with pre_get_posts, but post__not_in does work
- Can’t get full post title if there a spaces in title
- How to get all post_parents of a post?
- Query for first 3 posts to change the look and feel