Support » Fixing WordPress » Display posts between friday and thursday

  • Hi,
    I’m working on a movie schedule and I’m looking to create a Wp_Query to display posts between friday and thursday (the week after).
    So there will be a “Current schedule” where we could see movies of the week (friday to next thursday) and a “Future schedule” where we could see movies of the week after.
    How should I do? Use ACF custom fields?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The code below should be close to what you want:

    $time = time();
    // $time = strtotime('2012-10-23');  // Use this line to test certain dates
    $today = date('Y-m-d', $time);
    $day_of_week = date('w', $time);
    // If today is a Friday, get posts from today through next Thursday
    // Otherwise get posts from last Friday through the following Thursday
    if ($day_of_week == 5) {  //Today is a Friday
       $begin_date = $today;
       $end_date = date('Y-m-d', strtotime('next Thursday', $time));
    } else {
       $begin_date = date('Y-m-d', strtotime('last Friday', $time));
       // If today is a Thursday, use today's date else use next Thursday
       if ($day_of_week == 4) {  // Today is a Thursday
          $end_date = $today;
       } else {
          $end_date = date('Y-m-d', strtotime('next Thursday', $time));
    // Make sure BEGIN is from midnight and END is 11:59:59
    $begin_date .= ' 00:00:00';
    $end_date .= ' 11:59:59';
    echo "BEGIN:$begin_date  END:$end_date<br />";
    $sql = "SELECT * FROM $wpdb->posts
    WHERE post_type = 'post'
    AND post_status IN ( 'publish', 'future')
    AND post_date BETWEEN '$begin_date' AND '$end_date'
    ORDER BY post_date ASC, UPPER(post_title) ASC
    echo "<p>SQL:$sql</p>";
    $posts = $wpdb->get_results($sql);

    That will get you an array of posts – You’ll want to take that and create an array of post id’s, then feed that array into the ‘post__in’ arg of WP Query. That way you’ll be able to use pagination, and other nice features.

    That is unnecessary. You can paginate an array using paginate_links() and populate the $post object using setup_postdata().

    However, if you do want to use WP_Query(), you should modify the SQL to retrieve only the post IDs. No sense retrieving all of the data just to get the IDs. Change ‘SELECT *’ to ‘SELECT ID’.

    Good call! Although I’m a little confused as to how the pagination would work in the first solution. paginate_links() will only print out the links on the page, right? how will you let the custom query know which range of posts to show?

    I’ll give it a try!

    @bryan, here is an article showing how to use paginate_links() to paginate an array from a custom query:

    Pagination might not even be necessary for this case because you are retrieving posts in only a 6 day timespan.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display posts between friday and thursday’ is closed to new replies.