arjen Posted February 14, 2013 Share Posted February 14, 2013 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 More sharing options...
DaveP Posted February 14, 2013 Share Posted February 14, 2013 (edited) 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 February 14, 2013 by DaveP 1 Link to comment Share on other sites More sharing options...
arjen Posted February 14, 2013 Author Share Posted February 14, 2013 See post below. Link to comment Share on other sites More sharing options...
DaveP Posted February 14, 2013 Share Posted February 14, 2013 I have edited my code. As always there are many ways to do the same thing in PW. As far as pagination goes, you will need a LIMIT clause somewhere, I think. Link to comment Share on other sites More sharing options...
arjen Posted February 14, 2013 Author Share Posted February 14, 2013 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 More sharing options...
ryan Posted February 15, 2013 Share Posted February 15, 2013 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. 1 Link to comment Share on other sites More sharing options...
arjen Posted February 15, 2013 Author Share Posted February 15, 2013 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 More sharing options...
a-ok Posted October 15, 2015 Share Posted October 15, 2015 Any update on this? I'd like to show only pages where the date field hasn't passed yet: <?php $today = strtotime(date('Y-m-d')); $events = $pages->find('parent=/events/, date>$today, sort=date'); ?> Link to comment Share on other sites More sharing options...
tpr Posted October 15, 2015 Share Posted October 15, 2015 PW recognizes standard PHP date strings as I know, so this should work: <?php $events = $pages->find('parent=/events/, date>today, sort=date'); ?> https://processwire.com/api/selectors/ Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now