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
- Should we trust the post globals?
- How to get posts published between a date and today?
- Is it possible to Schedule Attachments in WordPress?
- How to know if get_posts() failed?
- Duplicate posts
- Looping through posts per category gives same posts for each category
- How to show posts rank based on custom field value
- How do I create Comma Separated list of attached image ids?
- 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?
- Should ‘setup_postdata()’ be reset with ‘wp_reset_postdata()’?
- Can wordpress differentiate between added and updated posts?
- How can I get all posts data from within a paginated search result?
- Check if post exists
- Only display posts after current date
- Improving WP_Query for performance when random posts are ordered
- Display post from specific date
- XML-RPC and post_date
- Replace ‘published on date’ with ‘modified on date’ on Posts
- pre_get_posts query between 2 dates (date stored in custom post meta)
- WP_Query Authors OR Categories
- do_shortcode within post query
- Update all comments time to random dates?
- get query() without post content?
- How can I setup a relationship using categories in WordPress?
- Exclude the first ‘n’ number of posts of a tag from home page?
- posts_per_page option limits the number of Gallery items
- How i can limit period of post publication?
- Worpdress function for difference dates
- Exclude current custom post on single post
- Dynamically switch template on click
- Get all custom post types excepted some…
- Get all comments of author’s posts
- Why Query is returning empty array?
- 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
- How to get all post_parents of a post?
- Query posts and filter at query time by value of custom meta
- Run query_posts if SESSION is empty?
- Very complex post query
- Delete post revisions only for a single post
- What date to use as a post date? date_gmt or modified_date_gmt
- How to give classname to post if post has no content?
- Exclude some posts from displaying in wp_query based on some condition
- Assigning Two Different Post Dates For Single Post
- Using system date format
- How to retrieve certain number of images from a wordpress post?
- Add “Posted on” to post date
- Auto update post title and slug when post status is changed
- How to convert query sql to shortcode in wordpress?
- Retrieving posts by their date and category
- Some doubts about how the loop work (trying debugging it)
- Date is wrong on ‘all posts’ page
- Plotting posts on a graph
- Adjust the order for returned posts
- get last post’s link with SQL query
- Trim posts from WP-Query?
- Trying to exclude custom posts based on date, while sorting by custom field
- Post date automatically +100 years into the future
- how to add tags to post in the Loop?
- Merge get_comments & get_posts into a single query
- How to optimize posts query in wordpress
- Change Old WordPress Post Date Year
- How to search through “post title” and “tags” using WP_Query?
- Set a post expiration and delete a post when expirate
- Display Updated Date Instead of Published Date
- I have two post types in the same query, how can I use CSS to select only one post type?
- How do you update post date (year only) in a separate custom field?
- Bulk Post update_post_meta
- How to hide meta from search result only on pages?
- How do I display posts ordered by a date custom field?
- Numbering author posts in posts query
- How to run select query of post with category and tags?
- Query posts from newest category
- Custom Select Query pagination not working properly
- Sort / Filter Queries
- Skip posts, but keep posts per page
- Query post category & remove any post id
- How to display an icon when a new post is published and then remove it when a specific time past?
- Display the latest post from some selected authors
- Ordering posts alphabetically by meta keys and title
- Why I have this strange behavior when I try to exlude the featured post from posts visualization?
- Grab next post title in archive page
- Continuous listing from a custom field
- How to organise post by category and date
- Displaying Results From Custom Taxonomy Query
- Display metabox with date
- How to store post rating system data post independent?
- Get first comment link on the post itself
- Make division in post_query?
- Display one post randomly from category on home page
- Display posts with id equal to relationship value
- Sort Posts Alphabetically Based on Specific Category (Divi)