Support

Account

Home Forums General Issues 'Daily Features' Posts by Date

Solved

'Daily Features' Posts by Date

    • adammm

    • July 10, 2013 at 11:57 pm

    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

    • Elliot

    • July 11, 2013 at 5:06 pm

    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

    • adammm

    • July 11, 2013 at 11:55 pm

    Thanks, Elliot. This totally worked. Much appreciated.

    • adammm

    • July 12, 2013 at 10:11 am

    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

    • Elliot

    • July 12, 2013 at 4:49 pm

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.