Support

Account

Home Forums General Issues 'Daily Features' Posts by Date

Solved

'Daily Features' Posts by Date

  • Hey! Long time lurker, first time posting.

    I am working on a restaurant website that has daily food features. My goal is to be able to publish the week’s worth of features, and have the homepage display that given day’s post automatically.

    Mon. – Ham
    Tues. – Turkey
    Wed. – Veggie
    ….

    I have setup a custom field for ‘Feature Date’ using the Date Picker.

    Here is what I have for the homepage:

    <?php query_posts( 'post_type=feature&posts_per_page=1'); ?>					
        <?php while (have_posts()) : the_post(); ?> 
        					<span class="medium-text">DAILY FEATURE FOR <?php the_time('l, F jS, Y') ?></span>
    
    					<span class="small-text"><?php echo get_the_title(); ?></span>
    					<span class="image-medium"><img src="<?=get_field('feature_image')?>" alt="Image"/></span> <?php endwhile; ?><?php wp_reset_query(); ?>  

    I really should probably have the ‘the_time’ be using the custom field. But I am unsure how to say ‘Find the post for 7/10/13 and display it’. And then the next day, find post for 7/11/13 and display it… and so on.

    Many, many thanks in advance.

    -Adam

  • Hi @adammm

    This task can be spit up into a few tasks:

    1. Find today’s date. This can be done with the date function is PHP. Please research this first, but it will look something like this:

    
    $today = date('Ymd');
    

    2. Next step is to add the date field to the ‘feature’ post type. It seems like you have already done this. Make sure the save_format is ‘yymmdd’ This JS format will match the PHP format of Ymd

    3. Lastly, just add in a meta query into the query_posts parameters. Please research this, but it will look something like this:

    
    <?php query_posts( 'post_type=feature&posts_per_page=1&meta_key=date_field_name&meta_value=' . $today); ?>	
    

    Hope that helps

  • Thanks, Elliot. This totally worked. Much appreciated.

  • It seems that either my server is off, or my code – but I set a Feature for today, but the site doesn’t show it. If I change it to be the feature for tomorrow, it works. So it’s like the PHP date is off. Does this have something to do with my shared server?

    Thanks,
    Adam

  • Hi @adammm

    Your local server will use your local time, however it is most likely that your live server is in a different timezone to you. I would read up on google and WP to learn how to take into account the time difference!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘'Daily Features' Posts by Date’ is closed to new replies.