Skip to content

Instantly share code, notes, and snippets.

@justingreerbbi
Created February 18, 2024 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justingreerbbi/9a97bb226fcd7e1390d50bdcc8ee4bc0 to your computer and use it in GitHub Desktop.
Save justingreerbbi/9a97bb226fcd7e1390d50bdcc8ee4bc0 to your computer and use it in GitHub Desktop.
Return WordPress posts for today or the last day there was posts
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