How to loop through categories to show most recent N posts

On my home page, I’m trying to use the Looper to get all categories that have at least one post, and the loop through to get the most recent N number of posts for each category. So it looks like this:

[Category title]
[Post 1] [Post 2] [Post 3] [Post 4]

[Category title]
[Post 1] [Post 2]

[Category title]
[Post 1]

The outer looper can only get all terms, with no exclusions which doesn’t help me, and the query builder/string brings back posts instead of just the list of category names/ids for me to iterate over. Is there a reference where I can see how to structure this to loop through the categories, getting the most recent N posts for each category to display? Thanks!

Hello @jrhager84,

Thanks for writing in!

You will need to run a nested looper. The element will be like this:

Looper Provider All Terms
   Looper Consumer
        Headline `{{dc:term:name}}` to display the category title
        Looper Provider Query String `post_type=post&cat={{dc:term:slug}}`
             Looper Consumer
                 Headline `{{dc:post:title}}` to display the post title

Hope this makes sense.

Ah. I think the issue is I was using category instead of cat.

Does this allow the exclusion of certain terms though? Like a category that I don’t want listed?

Hello @jrhager84,

If you want to exclude a category, you will have to add a condition in the first Looper Consumer element.

Thanks.

When I select Taxonomy is says ‘no options.’ It doesn’t seem to give me the ability to restrict a given category. Is that not something that can be done? I just want to filter certain categories to not appear in the search. Also - if I’m getting 3 and 1 is a filtered post, won’t it only bring back 2 then?

Hey @jrhager84,

We need to check your backend settings on how you set up the Loopers. With that said, please provide the following information in a Secure Note.

  • WordPress Login URL
  • Admin level username and password

You can find the Secure Note button at the bottom of your posts.

Hope that helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.