WordPress – Display a post a week in advance

  • I have a speakers category
  • Each speaker is set up as a post
  • The wordpress published date, is the date they are coming to speak (so most speakers posts are “scheduled”)
  • There is a new speaker each week (every 7 days)
  • On my speakers page I display the “featured speaker of the week” which contains all the post information
  • Underneath the featured speaker, is a list of upcoming speakers (just the title, not the whole post)

What I’m trying to accomplish is to have the “Featured Speaker”, only show up for a week (including the day they are speaking), and once the day after they have spoken comes, it will show the next person speaking on the site for a week until they have spoken and so on and so forth.

I’m getting stumped on the logic. I was doing well until I hit:

Read More

if($postdatenum >= $todaynum + 6)

Which will not work when it’s a different month.

I know my code is convoluted and probably outdated, but if anyone can shed some light on the logic for calculating the 7 days thing, that would be awesome.

Here’s what I have so far:

<?php 
    query_posts(array( 
        'post_type' => 'post',
    'category_name' => 'speakers',
    'showposts' => 1,
    'order' => DESC
    ) );  

while (have_posts()) : the_post(); ?>
<div class="">
<?php 
$today = date('m/d');
$postdate = the_date('m/d','','',FALSE);
//IF POST DATE AND TODAYS DATE NOT THE SAME
if($postdate != $today){

    wp_reset_query();
    query_posts(array( 
        'post_status' => 'future',
        'showposts' => 1,
        'order' => ASC
        ) );  
    while (have_posts()) : the_post();

    $todaynum = date('d'); //10
    $postdatenum = the_date('d','','',FALSE); //17

    if($postdatenum >= $todaynum + 6){
?>
        <h2><?php the_title(); ?></h2>
        <?php the_post_thumbnail(('medium'), array('class' => 'alignright')); ?>
        <?php the_content(); ?>

<?php
    }

    endwhile;
    wp_reset_query();

}else{
?>
    <h2><?php the_title(); ?></h2>
    <?php the_post_thumbnail(('medium'), array('class' => 'alignright')); ?>
    <?php the_content(); ?>
<?php
} endwhile;
?>

Related posts

Leave a Reply

2 comments

  1. I wonder if this trick with the date_query will work for you as a single query (untested):

    $args = array(
        'posts_per_page' => 2,
        'post_status'    => array( 'publish', 'future' ),
        'orderby'        => 'date',
        'order'          => 'ASC',
        'category_name'  => 'speakers',
        'date_query'     => array(
             array(
                 'before'    => '+13days',
                 'after'     => '-1days',
                 'inclusive' => true,
             ),
         ),
    );
    $query = new WP_Query( $args );
    

    The first post from this query should be the current speaker this week and the other post after that will be the upcoming speaker.

    If you want to include more upcoming speakers, you can increase the posts_per_page and increase the number of days in before parameter.

    So no need for more than a single query.

    ps: Don’t use query_posts(), it’s not recommended, check for example this quote from the Codex:

    Note: This function isn’t meant to be used by plugins or themes. As
    explained later, there are better, more performant options to alter
    the main query. query_posts() is overly simplistic and problematic way
    to modify main query of a page by replacing it with new instance of
    the query. It is inefficient (re-runs SQL queries) and will outright
    fail in some circumstances (especially often when dealing with posts
    pagination). Any modern WP code should use more reliable methods, like
    making use of pre_get_posts hook, for this purpose.

    Example:

    Let’s assume the lectures always starts at 12:00 pm each week.

    Then we mark the speakers (A, B, C, D, E, … ) on the calendar:

    Jan 2014                                                                                    Feb 2014
              A                  B                      C                      D                    E
    1 2 3 4 5 6 7 | 8 9 10 11 12 13 14 | 15 16 17 18 19 20 21 | 22 23 24 25 26 27 28 | 29 30 31 1 2 3 4 | 5 6 7 8 9 10
                      -  *  +  +  +  +    +  +  +  +  +  +  +    +  +   
                        -  *   +  +  +  +    +  +  +  +  +  +  +    +  +  
                           -  *   +  +  +  +    +  +  +  +  +  +  +    +  +  
                               -  *  +  +  +  +    +  +  +  +  +  +  +    +  +  
    

    Sorry for the mismatched +/- and the current day * in the above. It’s just an example how you can mark the calendar.

    Then we can construct the following list from the above query:

    Day Time Speaker
    ------------
    
     ...
    
     5 12:00 AB
     5 12:01 AB
    
     6 12:00 AB
     6 12:01 AB
    
     7 12:00 AB
     7 12:01 BC
    
     8 12:00 BC
     8 12:01 BC
    
     ...  
    
    12 12:00 BC
    12 12:01 BC
    
    13 12:00 BC
    13 12:01 CD
    
    14 12:00 CD
    14 12:01 CD
    
     ...