• Resolved mmachine

    (@mmachine)


    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…”

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mmachine

    (@mmachine)

    Ok, so I completely overlooked this from the PHP docs:

    ‘W’ | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)

    …which means there IS a much simpler way of doing this:

    $post_date = get_the_date();
    // Stringify the post dates
    $post_date = strtotime($post_date);
    // Declare first date in the query range
    $week_number = (int)date('W', $post_date);
    $year_number = (int)date('Y', $post_date);
    $post_query = 'w='.$week_number.'&year='.$year_number;
    query_posts($post_query);
    expresa

    (@expresa)

    Hello!
    I can read php but do not quite understand. Therefore I will ask for a help …

    I need to show the post, the last week. But I need to start the week on Thursday.

    That is, show the post between Thursday and Thursday.

    Anyone know how?

    Thanks!

    Alwyn Botha

    (@123milliseconds)

    Poor me ; running in circles

    cut, paste then search on

    Querying Posts in a Dynamic Date Range

    in google

    see it resolved; clicked it ; bring up this post

    Having a bad day

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Need Help Querying Posts in a Dynamic Date Range’ is closed to new replies.