admin管理员组文章数量:1022795
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;
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;
本文标签: plugin developmentHow to fetch only current hour posts
版权声明:本文标题:plugin development - How to fetch only current hour posts? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745514125a2153960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论