Skip Navigation

[Resolved] List of posts grouped by Year and Month.

This thread is resolved. Here is a description of the problem and solution.

Problem:
How could I create a Loop with posts grouped by Year and Month?

Solution:
https://toolset.com/forums/topic/use-toolset-to-create-a-list-of-links-for-year-and-month-archive-pages-and-count/#post-608249

If you require a adequate posts count as well to this YEAR/MONTH outputs, you can follow these steps as well:
https://toolset.com/forums/topic/use-toolset-to-create-a-list-of-links-for-year-and-month-archive-pages-and-count/#post-609342

Relevant Documentation:
https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

This support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 4 replies, has 2 voices.

Last updated by ashleighP 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#608140
Screen Shot 2018-01-19 at 7.55.03 PM.png

Tell us what you are trying to do? I want to create a list that shows the years for which there are existing blog posts and then a sub-list with the months for which there are existing blog posts, which links to each date-related archive page.

I've done this before with no problem for things like product categories + subcategories, but I can't find a way to list out the archive pages based on date. I can customize those archive pages using views, but not use a shortcode to link to them?

Is there any documentation that you are following? Views query by date; custom search; shortcodes for archives

Is there a similar example that we can see? Screenshot attached

What is the link to your site? hidden link

#608249

Here is as tech bulletin entry that explains how to do exactly what you need.
https://toolset.com/2013/10/how-to-group-views-results-by-year-and-month/

I tested this and it successfully groups Years (Output only once each) and Months, plus posts.

Front here it should be easy to group them in Additional HTML like accordions or link to other pages.

For reference, I will consult the code provided in the above bulletin with the developer as it seems wrong to me, and I needed to adjust the shortcodes a little:

<wpv-loop>
    [heading condition="year" value="[wpv-post-date format='Y']"]
      <h4>[wpv-post-date format="Y" id="[wpv-post-id]"]</h4>
    [/heading]
    [heading condition="month" value="[wpv-post-date format='F']"]
      <h5>[wpv-post-date format="F" id="[wpv-post-id]"]</h5>
    [/heading]
    <h4>[wpv-post-title][wpv-post-date]</h4>
</wpv-loop>

I added the Post ID

The rest worked as is.

Please let me know if this helps-

#609122
Screen Shot 2018-01-23 at 5.18.41 PM.png

I found this tutorial while I was searching support, but it doesn't do quite what I'm hoping for.

Because it's not actually applying any filter to the query result, the [wpv-items-count] shortcode doesn't return the number of posts in that month or year, it returns the total number of posts. (see screenshot)

#609342

I understand. This is not possible with this ShortCode.

I can customize those archive pages using views, but not use a shortcode to link to them?

Right, we do not have a ShortCode linking to either the Year archives, Month archives or Day archives.
But that should not be needed, you can craft a Custom Link similar to this:

<a href="[wpv-bloginfo show='url']/[wpv-post-date format='Y']">[wpv-post-date format="Y"]</a>

The post count thou is another issue.
Here we would need to alter each query but that is not possible within the Query Filters of the very View, these would affect all posts.
So you would need a second, nested View, with a Query that takes the date from a ShortCode attribute and returns only the Post Count.
Then, add the [wpv-items-count] above the Loop of this new View.
After, insert the View in the Loop of your first View, as this:

[wpv-view name="post-count-per-year" year="[wpv-post-date format='Y']"]

Unfortunately the feature you require is not inbuilt natively in Toolset, but it is achievable with above steps.

I can suggest this with a "adequate" amount of data. If you plan to have 10s of thousands of posts, I suggest to not use Toolset Views for this, as nesting the Views can result in a high load with the increasing amount of posts.
I speak of large amounts.

Please let me know if with these steps you are able to achieve the result you wish?

#609991

Yes, this works. Thank you!