Help to order and group posts by month (but not year)
-
I’ve been struggling with this for ages but can’t figure out how to order or group posts on a category page by month, ignoring the year completely.
I have a category ‘videos’
When viewing the category page/archive for ‘videos’ I need the posts to be grouped into months, with the current month’s posts displayed first (the videos are seasonal / time sensitive). The year needs to be ignored, so that in May (for example), the page will show all posts published in May of ALL years at the top.
I can do this for the current month using pre_get_posts and monthnum
$query->set('monthnum', $month);
, to modify the query on the category.php page, but as expected, ‘monthnum’ only shows posts from a month then stops. It doesn’t show the previous posts.I then need to show all posts from previous month ($month-1), and the month before that ($month-2) and the month before that etc etc
I guess I could do it by creating 11 additional queries and loops (for each previous month), but that can’t be the way to do it?
Unless I’m missing something I don’t think theres a way to do anything like
orderby -> monthnum
…What’s the smart way? Hope someone can help!
Many thanks
- The topic ‘Help to order and group posts by month (but not year)’ is closed to new replies.