-1
<?php if ( have_posts() ) : ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
else :
get_template_part( 'content', 'none' );
endif;
?>

A simple loop her eon my index page. I am using the No Future Posts plugin to publish posts with dates in the future but the most relevant ones are today.

I have been doing a ton of cutting and pasting code from around the net trying to find a way to get just todays posts to show but nothing is sticking. I'm sure I need to check for todays date then put it into the loop but I have no idea how to do that, I know very little php, I usually just copy and paste other peoples code.

If any of the experts on here could maybe point me in the right direction it would be appreciated.!

3
  • I read through that and the people on there seem confused what theyre talking about so I have no chance Commented May 18, 2016 at 2:01
  • I am trying, I'm also starting from scratch. Where do I put the code under the section labelled "Returns posts for today:" ? Commented May 18, 2016 at 2:26
  • Always file edits to your question when try something (and fail). Describe what you tried and what exactly failed. "Where do I put the code" probably is not the best question to ask on a development site.
    – kaiser
    Commented May 29, 2016 at 14:32

1 Answer 1

7

If you just want posts from today, it's super easy and right in the WP codex:

<?php
$today = getdate();
$args = array(
    'date_query' => array(
        array(
            'year'  => $today['year'],
            'month' => $today['mon'],
            'day'   => $today['mday'],
        ),
    ),
);
$custom_query = new WP_Query( $args );
?>

<?php if ($custom_query->have_posts()) : while ($custom_query->have_posts()) : $custom_query->the_post(); ?>

  <?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; endif; // end of custom loop ?>
<?php wp_reset_postdata(); ?>
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.