adrian Posted March 13, 2013 Share Posted March 13, 2013 Hi everyone, Trying to simply find pages where the modified date is greater than the created date. I would think this should work: $pages->find("modified>created"); but no luck. Thanks for any suggestions. Link to comment Share on other sites More sharing options...
diogo Posted March 13, 2013 Share Posted March 13, 2013 Try this: $allPages = $pages->find("anySelector"); foreach($allPages as $p) if($p->modified == $p->created) $allPages->remove($p); //I'm using == instead of <= because modified would never be smaller than created $allPages should hold your desired results now 2 Link to comment Share on other sites More sharing options...
adrian Posted March 13, 2013 Author Share Posted March 13, 2013 Thanks diogo - a nice solution - I hadn't thought about the remove option. Only catch is that my find makes use of a limit selector. Obviously I can break the foreach loop after my limit requirement, but it will mean grabbing all the pages that match and then not using most of them in the foreach - just seems wasteful. Link to comment Share on other sites More sharing options...
ryan Posted March 14, 2013 Share Posted March 14, 2013 Selectors have to have a defined value after the operator. There is no defined value in "modified>created", as those are two fields. You can go straight to SQL to accomplish this one: $pageIDs = array(); $result = $db->query("SELECT id FROM pages WHERE modified>created"); while($row = $result->fetch_row()) $pageIDs[] = $row[0]; $pageArray = $pages->getById($pageIDs); // $pageArray contains what you want 6 Link to comment Share on other sites More sharing options...
adrian Posted March 14, 2013 Author Share Posted March 14, 2013 Thanks Ryan, I have been trying to avoid using SQL directly in PW - just figured there was something in the API I was missing, but it seems that in some cases it will be more efficient. Link to comment Share on other sites More sharing options...
diogo Posted March 14, 2013 Share Posted March 14, 2013 You could also create a module that would hook to save and update a field holding the value of modified minus created. Then you could use $pages->find("modified-created>0"); 3 Link to comment Share on other sites More sharing options...
DZoneRick Posted March 15, 2013 Share Posted March 15, 2013 Selectors have to have a defined value after the operator. There is no defined value in "modified>created", as those are two fields. You can go straight to SQL to accomplish this one: $pageIDs = array(); $result = $db->query("SELECT id FROM pages WHERE modified>created"); while($row = $result->fetch_row()) $pageIDs[] = $row[0]; $pageArray = $pages->getById($pageIDs); // $pageArray contains what you want Awesome! I had no idea it was so easy to drop straight to SQL. You continue to surprise me with practical, useful solutions! Best, Rick 1 Link to comment Share on other sites More sharing options...
diogo Posted March 15, 2013 Share Posted March 15, 2013 Here is a draft of the module that I talked about above <?php //ModifiedMinusCreated.module class ModifiedMinusCreated extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Modified minus created', 'version' => 100, 'summary' => 'only a rough draft', 'singular' => true, 'autoload' => true, ); } public function init() { $this->pages->addHookBefore('save', $this, 'subtract'); } public function subtract($event) { $page = $event->arguments[0]; if(!$page->fieldgroup->modifiedminuscreated) return; $page->modifiedminuscreated = $page->modified - $page->created; } Now you just have to create the integer field "modifiedminuscreated" and add it to the templates you want. edited: changed the code under Soma's suggestion 2 Link to comment Share on other sites More sharing options...
Soma Posted March 15, 2013 Share Posted March 15, 2013 $page->done isnt needed. :-P Link to comment Share on other sites More sharing options...
diogo Posted March 15, 2013 Share Posted March 15, 2013 true, I will change that Link to comment Share on other sites More sharing options...
adrian Posted March 16, 2013 Author Share Posted March 16, 2013 Hey diogo - cool idea and a great example of how easy it is to create a module. Looking forward to finding a little time to start creating my own soon. Thanks again. Link to comment Share on other sites More sharing options...
NorbertH Posted October 9, 2018 Share Posted October 9, 2018 (edited) Still it would be great to have options like "modified>created" or "modified=created" in selectors . That would make it very comfortable. Maybe whith putting the second selector into some brackets, to mark it as selector and not value. "modified={created}" So possibly allowing sub selects in selectors. Edit: sub Selectors are already implemented .. http://processwire.com/api/selectors/ Edited October 9, 2018 by NorbertH sorry most of this is already implemented 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