Home › Forums › General Issues › Order Post By Year, and Then 3 Months Period
Hi, I have an news page and want to list the post by year and then 3 period months.
I’m looking to do something like this:
2020
October – December
– News
– News
July-September
– News
– News
April-June
– News
– News
January-March
– News
– News
2019
October – December
– News
– News
July-September
– News
– News
….
Can anyone help me?
<?php
$currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
/* Order Posts based on Date Picker value */
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'jmt_news', // name of custom post type
'meta_key' => 'date', // name of custom field
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_status' =>'publish',
'meta_query'=> array(
array(
'key' => 'date',
'compare' => '<',
'value' => $currentdate,
'type' => 'DATE',
))
));
$years = array();
$months = array();
$groupMonths = [
'October-December' => ['october', 'november' , 'december'],
'July-September' => ['july', 'august' , 'september'],
'April-June' => ['april', 'may' , 'june'],
'Januari-March' => ['january', 'february' , 'march']
];
if( $posts )
{
foreach( $posts as $post )
{
setup_postdata( $post );
// get date
$date = date_create( get_field('date') );
// get year
$year = date_format($date,'Y');
$month = date_format($date,'F');
$group_posts[$year][$month]['posts'][] = $post;
}
wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}
foreach ($group_posts as $yearKey => $years) {
echo '<p>'.$yearKey.'</p>' ;
foreach ( $groupMonths as $groupMonth => $monthArr) {
echo '<p>'.$groupMonth.'</p>';
foreach ($monthArr as $months) {
foreach ($years as $months) {
if( $months['posts'] ){
foreach( $months['posts'] as $post ){
setup_postdata( $post );
echo '<p>';
echo the_title();
echo '</p>';
}
}
}
}
}
}
?>
If this is a date field (not a date time field) then your issue lies in the fact that ACF does not store “Date” values in the DB for the field. The values are in the format of “YYYYMMDD”. You’re queries must be on either “CHAR” or “NUMERIC” comparisons and any calculations based on the date must use this format.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.