Created
February 18, 2024 14:25
-
-
Save justingreerbbi/9a97bb226fcd7e1390d50bdcc8ee4bc0 to your computer and use it in GitHub Desktop.
Return WordPress posts for today or the last day there was posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global $wpdb; | |
/** | |
* Get todays date and the last post date to compare | |
*/ | |
$query_date = date( 'Y-m-d' ); | |
$last_date = $wpdb->get_var( "SELECT DATE(MAX(post_date)) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = 'post' LIMIT 1" ); | |
// Check if the last post is from today or not | |
if ( $last_date != $query_date ) { | |
$query_date = $last_date; | |
} | |
// Build the query | |
$args = array( | |
'date_query' => array( | |
array( | |
'year' => date( 'Y', strtotime( $query_date ) ), | |
'month' => date( 'n', strtotime( $query_date ) ), | |
'day' => date( 'j', strtotime( $query_date ) ), | |
) | |
), | |
'posts_per_page' => -1, | |
); | |
$p = new WP_Query( $args ); | |
wp_send_json( $p->posts ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment