Why does this query not work correctly?

Dear community,
I’m using the following query on my homepage. The query should check if there is an entry, which isn’t older that 30 days. However, when there is more than one post from the same day, it only shows the first one of the single day. For example, there is a post from today at 8am and a second one from 4pm, it only shows the 8am one, not the 4pm one. Any idea whats wrong with my query? I guess it needs to be filtered by time instead of date?

<?php if ($short = page('shorts')->children()->published()->filterBy('date', 'date >', date('Y-m-d', strtotime('- 30 days')))->flip()->first()) : ?>

I think you dont need the word date in the operator…try this…

<?php if ($short = page('shorts')->children()->published()->filterBy('date', '>', date('Y-m-d', strtotime('- 30 days')))->flip()->first()) : ?>

See more filtering examples here Filtering collections | Kirby CMS

If you fetch only one entry (first()), then you get only one entry. The filter query itself looks ok.

So, to get multiple, the query should look like this:

$shorts = page('shorts')->children()->published()->filterBy('date', 'date >', date('Y-m-d', strtotime('- 30 days')))->flip();

if ($shorts->isNotEmpty()) {
    foreach ($shorts as $short) {
       // do stuff
    }
}

Oh for goodness sake! didnt even see that. :eyes:

Thanks for all your responses so far. Maybe i’ve misspelled it. I only want one entry, the problem is that it doesn’t show the most recent one, when there are multiple posts per day. I want to show the most recent one, i guess you could say the “last” entry. Like i said before: If there is a post from today at 8am and a second one from 4pm, it only shows the 8am one, not the 4pm one. If the post is on another day, it works without any problems.

You are flipping the order with flip(), then remove that.

So this should work?

<?php if ($short = page('shorts')->children()->published()->filterBy('date', '>', date('Y-m-d', strtotime('- 30 days'))) : ?>

Edit: It doesn’t. I’m not really a programmer, so it’s dificult for me. I need to find out the logic first :slight_smile:

Na, now you have removed first() as well… Only remove the flip part and leave everything else exactly as it was in your first post.

But to check if you get all results, you could first do a

dump(page('shorts')->children()->published()->filterBy('date', 'date >', date('Y-m-d', strtotime('- 30 days'))));

Interesting. When i dump the posts, i get the following result:

Kirby\Cms\Pages Object
(
    [0] => shorts/oldest-article
    [1] => shorts/second
    [2] => shorts/newest
    [3] => shorts/third

Shouldn’t the newest be at 3rd position? and the third one at the second position? Looks like the order is wrong

What sort of numbering scheme do you use for your shorts when they are listed? Are they even listed (you query published which included listed and unlisted pages, but not drafts).

Default sorting is by order in the file system, unless you sort them by something, the most logic sorting would be by date here.

On the page, where i list all posts, i use:

<?php $shorts = page('shorts')->children()->sortBy('date', 'desc')->limit(60); ?>

And that one works correct. Do i need the “desc” in my other query as well maybe?

Well, the desc alone won’t help, your query doesn’t include a sort statement

1 Like

I got it working. The solution was:

<?php if ($short = page('shorts')->children()->published()->sortBy('date', 'desc')->filterBy('date', 'date >', date('Y-m-d', strtotime('- 30 days')))->first()) : ?>

Thank you :slight_smile: