1

I want to get posts of a specific date, I tried the following code but I did not get the result what I wanted.

<?php
$mil = 1532996880000;
$seconds = $mil / 1000;
$dt = date( "Y-m-d", $seconds );

$post_by_date = $wpdb->get_row("
    SELECT * FROM {$wpdb->posts}
    WHERE post_date = $dt
    AND post_status = 'publish'
");

WP QUERY

$posts = get_posts(array(
    'post_type' => 'post',
    'date' => $dt,
    'posts_per_page' => 1
));

This did not work because, the output of $dt = 2018-07-31, but the output shows no results. Because it compares with 2018-07-31 09:09:40. So is there any way to get posts of a specific date?

1
<?php
//  primarily get $year, $month and $day
$args = array(
    'date_query' => array(
        'year'  => $year,
        'month' => $month,
        'day'   => $day,
    )
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {

        // do whatever here

    }
}
  • Ok, I think this is the only way I can do it. Thank you Max – topper1309 Aug 3 '18 at 7:18
1

Please use date query for that also use below code hope it will work for you

$mil = 1532996880000;
$seconds = $mil / 1000;
$dt = date( "Y-m-d", $seconds );
 $year =  date( "Y", $seconds );
 $month =  date( "m", $seconds ); 
 $day =  date( "d", $seconds );


$args = array(
 'date_query' => array(
  array(
   'year'  => $year,
   'month' => $month,
   'day'   =>$day,
  ),
 ),
);
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    echo '<ul>';
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
} else {
    // no posts found
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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