Support » Fixing WordPress » separate query results by "week of" in a custom post type

  • I have a Custom post type of “events” and I have a custom metabox for the date of the event. All events added with all dates recorded into database in srtotime() UNIX format.

    When list of events is queried I need to separate them by week. So.. “week of 4/23” then list the events for that week only.. followed by “Week of 4/30” with the events for that week listed.

    All of this needs to be dynamic and logically detect when each week begins and compare that to the date that is saved in the database for the returned events, thus grouping them into “weeks.”

    I hope that all makes sense. I am just having trouble finding resources for this type of functionality, and I am uncertain of the logic needed and what WP might already have I could use.

    Thanks in advance for helping any way you can!

Viewing 1 replies (of 1 total)
  • Here is a code snippet that should get you started:

    $tm = strtotime($post->post_date);
          $dow = date('w', $tm);
          $dnm = date('D', $tm);
          $new_wk = date('m/d',strtotime("$post->post_date - $dow days"));
          if ($curr_wk != $new_wk) {
             // Start a new week
             $curr_wk = $new_wk;
             echo "<h2>Week of $curr_wk</h2>";
    
          }
          echo "<p>POSTDT: $post->post_date  DNM: $dnm  DOW:$dow CURRWK: $curr_wk  NEWWK:$new_wk</p>";

    Of course, you will need to use your own datetime instead of converting the post_date.

    And this shows weeks as beginning on Sunday so it will need some adjusting if you want the week to start on Monday.

Viewing 1 replies (of 1 total)
  • The topic ‘separate query results by "week of" in a custom post type’ is closed to new replies.