How can I fetch the post which is posted only this hour?
Example: It's 9:00 am here I want to show all the post that is posted within 9:00am to 10am and it will check all the hours in same way.
How can I fetch the post which is posted only this hour?
Example: It's 9:00 am here I want to show all the post that is posted within 9:00am to 10am and it will check all the hours in same way.
Share Improve this question edited May 8, 2019 at 4:13 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked May 8, 2019 at 3:40 Mahbub AnsaryMahbub Ansary 312 bronze badges2 Answers
Reset to default 0I think Date Parameters is the thing you’re looking for.
So if you want to get posts from given hour, you can construct a query like this:
$hour_posts = new WP_Query( array(
'date_query' => array(
array(
'before' => '2019-05-08 10:00',
),
array(
'after' => '2019-05-08 09:00',
),
),
'posts_per_page' => -1,
) );
And if the day doesn’t matter and you want all posts published within given hour but from all days, then this will come handy:
$args = array(
'date_query' => array(
array(
'hour' => 9,
'compare' => '>=',
),
array(
'hour' => 10,
'compare' => '<'
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
Solved!!!
//set up the round figure of this hour
$timeWithoutA = date('h:')."00";
$curr_time = date('h').":00".date('a');
//Add 1 hour to current hour
$timestamp = strtotime($timeWithoutA) + 60*60;
$addedOneHour = date('h:ia', $timestamp);
//Today's date
$this_date = getdate();
$year = $this_date['year'];
$mon = $this_date['mon'];
$day = $this_date['mday'];
$today = $year."-".$mon."-".$day;
//Add them together
$after = $today." ".$curr_time;
$before = $today." ".$addedOneHour;
$custArgs = array(
'post_type' => 'payment',
'date_query' => array(
array(
'before' => $before,
),
array(
'after' => $after,
)
)
);
$query = new WP_Query ( $custArgs );
while($query->have_posts()) : $query->the_post();
echo "hello"."<br />";
endwhile;