Jump to content

Selector with dates and checkboxes


arjen
 Share

Recommended Posts

Hi,

I've strangled my brains around - what it seems easy selector issue - but I can't seem to get the right results. The situation is:

I've got a template called 'post' with a checkbox field called 'frontpage' and a date field called 'max_date'. The frontpage checkbox is to let the editor know to place the article on the homepage and the max_date is to give a post an optional maximum date on the frontpage so the post will not show if that date has passed.

On the homepage I would like to load:

The last created 10 posts with the checkbox frontpage checked and if there is a max_date check if it's greated than today otherwise load the post.

The following code works, but won't load the post with no max_date filled in. I could use the ->not() function, but then the limit won't work.

$today = strtotime( date('Y-m-d'));$pages->find("template=post, frontpage=1, max_date>=$today, limit=10, sort=-created");
I'm on PW version 2.2.9.

Any thoughts? Thanks!

edit: I would like to use pagination.

Link to comment
Share on other sites

There is probably a better way just using 1 selector string, but you could do two, and then sort the resulting pagearray -

$my_pages = $pages->find("template=post, frontpage=1, max_date>=$today, limit=10");
$my_pages .= "|" . $pages->find("template=post, frontpage=1, max_date='', limit=10");
$my_pages->sort("-created");

(Written in browser, but should work.)

***Edited since arjen's reply below but still doesn't work. I give up  :(

Edited by DaveP
  • Like 1
Link to comment
Share on other sites

Thanks DaveP,

I should've noticed that I'm using pagination on the homepage. Your code results in the following id's: 1016|1018|1057|10591020. Notice the last id is missing a pipeline.

Another options would be:

$today = strtotime( date('Y-m-d'));$pages->find("template=post, frontpage=1, max_date>=$today")->add($pages->find("template=post, frontpage=1, max_date=null"));

But then my pagination won't work. If I can get a limit=10 in the selector the pagination will work. Food for thought!

As always there are many ways to do the same thing in PW.
 

True. Thanks for your ideas.

Link to comment
Share on other sites

Eventually we will support this type of OR expression in selectors. But currently you can't do this: (max_date>=$today OR max_date=0). So the current strategy of your setup does not lend itself well to pagination in ProcessWire. What I recommend doing instead is one of the following:

  • Have a separate checkbox for "frontpage_always" or something like that. When checked, it is always used, regardless of max date setting.
  • Rather than checkbox toggles, use page references to provide options like: "Frontpage till max date", "Frontpage always", and possibly others. 

Another thing to note is that you don't need to convert $today to a timestamp (though it doesn't care if you do). You can just pass in the YYYY-MM-DD string and it'll be fine with that too. 

  • Like 1
Link to comment
Share on other sites

Thanks Ryan, How would you build the selector using the frontpage_always strategy? (Or even the page reference strategy)

$pages->find("template=post, frontpage_always=1, max_date>=$today");
will still only return items with the max_date greather than today. So posts without a date but with the checkbox frontpage_always will be discarded. Thanks for your note about the timestamp thing. Didn't knew that!

Additional:

I think i've found another way to fix this. It feels hackish, but the following seems to work.

$latestPosts = $pages->find("template=post, frontpage=1, max_date>=$today")->add($pages->find("template=post, frontpage=1, max_date=null"));

$pages->find("id=$latestPostsMaxDate, limit=3, sort=-created");

Now both the checkbox and the maximum date seems to work.

Link to comment
Share on other sites

  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...