admin管理员组文章数量:1130349
I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.
So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)
Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?
Thanks
/Anders
I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.
So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)
Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?
Thanks
/Anders
Share Improve this question asked Dec 30, 2018 at 23:10 Anders OlsenAnders Olsen 1 2- A couple of other options without re-publishing- you can get the current date and query for a post on the same day in the year it was originally published. or you could use a taxonomy or post meta to store a date, and similarly query for the current day's post. – Milo Commented Dec 31, 2018 at 1:51
- HI Milo, I'm sorry, but I don't fully understand what you mean... could you perhaps give me an example so it would be easier for me to implement? Thanks a lot /Anders – Anders Olsen Commented Jan 1, 2019 at 22:06
1 Answer
Reset to default 1Rather than re-post the posts, we can create a custom query for the current day's post regardless of year. We use current_time to get the current day and month according to the site's timezone settings, then we create a new query containing date parameters for month and day. We don't specify a year, so it'll return anything posted on this day from any year.
$day = current_time( 'j' );
$month = current_time( 'n' );
$args = array(
'date_query' => array(
array(
'month' => $month,
'day' => $day,
),
),
);
$today = new WP_Query( $args );
if ( $today->have_posts() ) {
while ( $today->have_posts() ) {
$today->the_post();
// output the post
the_title();
}
wp_reset_postdata();
}
I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.
So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)
Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?
Thanks
/Anders
I am creating a website about 'This Day in History' where I show daily events etc. all based on a particular date. Each post has to be re-posted every year, and a specific date so that everyday users see information relevant to today's date.
So if I have 365 posts, one for each day, each post must be the published post for that specific date. In other words, posts kind of rotate every day throughout the year. So every day a new post is published, the very post that matches that date... I hope I make my self clear :-)
Is there a way I can control how the posts are (re)-scheduled, so that they continue to get posted each year on that specific date?
Thanks
/Anders
Share Improve this question asked Dec 30, 2018 at 23:10 Anders OlsenAnders Olsen 1 2- A couple of other options without re-publishing- you can get the current date and query for a post on the same day in the year it was originally published. or you could use a taxonomy or post meta to store a date, and similarly query for the current day's post. – Milo Commented Dec 31, 2018 at 1:51
- HI Milo, I'm sorry, but I don't fully understand what you mean... could you perhaps give me an example so it would be easier for me to implement? Thanks a lot /Anders – Anders Olsen Commented Jan 1, 2019 at 22:06
1 Answer
Reset to default 1Rather than re-post the posts, we can create a custom query for the current day's post regardless of year. We use current_time to get the current day and month according to the site's timezone settings, then we create a new query containing date parameters for month and day. We don't specify a year, so it'll return anything posted on this day from any year.
$day = current_time( 'j' );
$month = current_time( 'n' );
$args = array(
'date_query' => array(
array(
'month' => $month,
'day' => $day,
),
),
);
$today = new WP_Query( $args );
if ( $today->have_posts() ) {
while ( $today->have_posts() ) {
$today->the_post();
// output the post
the_title();
}
wp_reset_postdata();
}
本文标签: loopRepost post on specific date every year
版权声明:本文标题:loop - Repost post on specific date every year 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749052431a2308826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论