Using WP_Query To Get Posts Randomly From today

In the logic of the data_query(); I found this WP_Query sample:

<!-- Second Senses Featured -->
    <ul id="featuredSecond" class="featured-second">

      <?php
      $today = getdate();
      $args = array(
        'tag_slug__in' => array( 'destacado'),
        'posts_per_page' => 2,
        'post_type' => 'any',
        'offset' => 3,
        'orderby' => 'rand',
        'date_query' => array(
          array(
            'after' => 'Today',
            'inclusive' => true,

            )
          ),
        'post_status' => array(
          'publish'
          )
        );

      $featured = new WP_Query($args);

      if ( $featured->have_posts() ) :
        while ( $featured->have_posts() ) : $featured->the_post();
      ?>

      <li id="<?php the_slug(); ?>-<?php the_id(); ?>">
        <article id="post-<?php the_id(); ?>" class="cf"  role="article">

          <?php featured_content('thumb-480x300'); ?>

          <div class="featured-box">
            <h1 class="featured-title cf">
              <a href="https://wordpress.stackexchange.com/questions/172576/<?php the_permalink() ?>">
                <?php the_title(); ?>
              </a>
            </h1>
          </div>
          <!--featured-box-->

        </article>

      </li>
      <?php
      endwhile;
      endif;
      wp_reset_postdata();
      ?>

But everything is rotating with post with 3 week old.. or more!

how this logic is not working?

if you notice there is a variable called $today , but I didn’t make it work…

thanks

3 Answers
3

I don’t think there is a parameter value Today as you used in date_query.

If you want to return today’s post then you should provide date value to date_query as you stored current date in $today array. So here is your query now.

      $today = getdate();
      $args = array(
        'tag_slug__in' => array( 'destacado'),
        'posts_per_page' => 2,
        'post_type' => 'any',
        'offset' => 3,
        'orderby' => 'rand',
        'date_query' => array(
            array(
              'year'  => $today['year'],
              'month' => $today['mon'],
              'day'   => $today['mday'],
            ),
          ),
        'post_status' => 'publish',
        );

        $featured = new WP_Query($args);

Leave a Comment