Need Help Querying Posts in a Dynamic Date Range
-
What I need to do is find the date of a specific post, determine the dates of its surrounding Sundays, and then query all other posts that were published in that date range to be displayed. I understand the basics of date math in PHP, but I’m really having a hard time getting this code to play nice with WP query_posts() function. Here’s my code so far:
$post_date = get_the_date(); // Stringify the post dates $post_date = strtotime($post_date); // Declare first date in the query range $first_sunday = date(strtotime("last Sunday",$post_date)); // Declare second date in the query range $second_sunday = date(strtotime("+1 week",$first_sunday)); // Query posts with respective dates $week_number = (int)date('W', $first_sunday); echo $week_number; query_posts('w='.$week_number);
What would be great is to query by week number instead of filtering an entire where statement, as seen here — https://codex.www.ads-software.com/Function_Reference/query_posts. I think though, the “week” as PHP reads it doesn’t take into account a Sunday-Saturday week — it’s just a strict numbering in 7 day increments across a span of 365 days.
Am I overlooking a totally simple way to do this? The plan for execution is on an the archive pages — for each post rendered, I want to display any posts published in that same date range as well. ie, “Also Posted That Week…”
- The topic ‘Need Help Querying Posts in a Dynamic Date Range’ is closed to new replies.