Hi all,
Is there a simple way to filter the last post of each category in a query loop ?
I have 6 categories, and I would like to display 6 post, the last published post of each category.
Do you have an idea ?
Hi all,
Is there a simple way to filter the last post of each category in a query loop ?
I have 6 categories, and I would like to display 6 post, the last published post of each category.
Do you have an idea ?
Maybe with PHP:
<?php
$categories = get_categories();
foreach ($categories as $category) {
$category_id = $category->term_id;
$args = array(
'cat' => $category_id,
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
);
$query = new WP_Query($args);
....
Thanks Sebastien for your help, but I’ve been struggling with the PHP code, the problem of calling up publications and finally if I have to set it up in pure PHP with HTML it adds quite a bit of work with CSS and responsive. As I wanted to finish my job quickly because I had to deliver within the day, I duplicated the publication module, limiting it to 1 post and a category with a filter on the last post. However, I’d really like to be able to improve it by using the great Query Loop conditional module from Bricks.
Yes Patric, I tried with the new Bricks PHP editor which would be the best, but I couldn’t display only the last post of each category. I must have inserted the wrong parameters for the PHP query.
Here’s my query, maybe an advanced PHP user will quickly find the solution :
return [
‘post_type’ => ‘post’,
‘posts_per_page’ => ‘6’,
‘category_name’ => ‘actualites,entreprises,high-tech,loisirs,marketing,shopping’,
‘post_status’ => ‘publish’,
‘orderby’ => ‘date’,
];
But with these settings I have 2 posts from the “Shopping” category displayed and not the post from the “Marketing” category.
I think you can achieve it with two query loops, an outer and an inner loop. The outer loop querys the taxonomies . The inner loop querys the posts. For the inner loop you need to apply the taxonomy-query-setting and for the condition you chose {term_name} that is coming from the outer loop. Also you need to order the inner loop by date in descending order and limit the number of posts per page to 1 . That should work
Yeah, I think that (two query loops) is the solution that Cyril used.
I was playing around yesterday trying to find an easier out-of-the-box solution WITHOUT using a custom query.
But every solution I found was either using code, a Bricks query filter or at the end it was not easier than his current solution.
Maybe somebody else has an idea how to achieve this in the most simple way and without using code or a Bricks query filter. Just for fun.
Cheers
Patric
Cheers
Patric
Hey Patric,
I don’t see any information that he used two query loops. I set up a test with my solution and that worked just fine and is easy to do, no custom code or filter required. His current solution is not working because it’s not selecting the last post of each category but is selecting instead the last 6 posts that are in any of those categories. That’s why it’s happening that two of the same categories (in his example from category ‘shopping’) are in the output. With this solution it would even be possible to have 6 of just one category (depending on the dates of the posts).
Br
Tobias
Hi Tobsen
I understand from his comment in his post “…As I wanted to finish my job quickly because I had to deliver within the day, I duplicated the publication module, limiting it to 1 post and a category with a filter on the last post…” that he did it via two loops and the last loop using the category filter like you mentioned.
Yes, your solution works perfectly. I just wonder if it can also be done any simpler…
Cheers
Patric
Hi Patric and Tobsen,
First of all, I’d like to thank you for your involvement, your kindness and the time you’ve taken to help me solve this problem. You are great guys. I’m going to try your 2 query loops solution right away and I’ll keep you posted.
You may seem like a beginner, but I’ve just acquired Bricks builder recently.
Simple question : how do I create two query loops (external loop and internal loop) ?
One query loop no problem, but two query loops ???
Do I create them on the publication module ? Or do I create them on a “block” and set the title/category and date elements dynamically with text or titles ?
Right now I’m on the publication module. Do you have a screenshot for 2 query loops so I can understand ?
Hey Cyril,
you take a block-element (let’s name it ‘Outer Block’) and set a query loop on it (outer query loop). Then you add another block element as a child to ‘Outer Block’ and set another query loop on that element (inner query loop). So you have two distinct nested layout-elements, each with their own query loop.
You can then use dynamic data to output whatever post-data you’d like to use (see Dynamic Data – Bricks Academy ). For example post-title is {post_title} .
Hope that made things clear, if not feel free to ask!
br
Tobias
Structure
Outer-Loop-Settings
Inner-Loop-Settings 1
Inner-Loop-Settings 2
Of course you can refine the loop settings for your specific needs, f.e. in the screenshots I forgot to set post per page to 1 in the inner loop (you need to do that!) and you can select your specific terms in the outer loop
Hey Tobsen,
It works perfectly, you are the King of Bricks !
Now I’ll get to work on the layout.
I’ll post a screenshot when I’ve finished.
Many thanks to Tobsen.
Thanks again to Tobsen for helping me finalize the possibility of publishing the last post of each category in query loop mode.
I’ve also included a simple solution with element duplication. Just change the category in each element.
Regards
Cyril
Well done, looks great! All the best!
Tobias
Sorry to reopen this old topic, how would one achieve this for a cpts and custom taxonomies?
I created a Custom taxonomy called ‘Themes’
This is out I set it up:
Outer loop: https://i.imgur.com/zcWZo60.png
Inner loop: https://i.imgur.com/8kILCnX.png and https://i.imgur.com/3UhkmeT.png
But it didn’t work with {post_terms_customthemes} so I put {term_name} but not sure if that is working. because it shows repeated posts
Have you found a solution since?
If not, you’ll need @Tobsen’s help, he’s really very talented.