mel47 Posted May 6, 2019 Share Posted May 6, 2019 Hi, I have difficulty to find the good selector. Home ----Categories ------------categ -----Activities ----------activity (with a pageref to categ) I create a search form with multichoice to listing all categories ($categ = $pages->find("parent.id=1057, sort=title")). However, some categories are currently not used and I don't want them to be listed. How I can modify my selector the more efficient way? I was thinking of searching categories in all activity's pages and recreate the array. Or exclude pages category without references to them. Or something else simpler I don't know?? Thanks Mel Link to comment Share on other sites More sharing options...
arjen Posted May 6, 2019 Share Posted May 6, 2019 Do these unused categories also have an empty pageref to categ? A selector like: "template=activity, pageref.count>0" will only return activities with filled page reference to categ. See more here: https://processwire.com/docs/selectors/#count Link to comment Share on other sites More sharing options...
Robin S Posted May 6, 2019 Share Posted May 6, 2019 Here is a hook method for /site/ready.php that gets the pages that are selected in a given Page Reference field: /** * Get pages selected in a Page Reference field * Use optional argument $options for limiting 'template' and/or 'parent_id', and to 'include_unpublished'. * Usage: $selected_pages = $fields->my_page_field->getSelectedPages(['template' => 'basic_page']); * The number of times each page is selected is available in a custom "occurrunces" property on each selected page. */ $wire->addHookMethod('Field(type=FieldtypePage)::getSelectedPages', function(HookEvent $event) { /* @var Field $field */ $field = $event->object; $options = $event->arguments(0); $get_options = []; if(isset($options['template'])) $get_options['template'] = $this->wire('templates')->get($options['template']); if(isset($options['parent_id'])) $get_options['parent_id'] = (int) $options['parent_id']; $table = $field->getTable(); $query = $this->wire('database')->query("SELECT data FROM $table"); $ids = $query->fetchAll(\PDO::FETCH_COLUMN); $count_values = array_count_values($ids); $items = $this->wire('pages')->getById(array_keys($count_values), $get_options); foreach($items as $item) { if(empty($options['include_unpublished']) && $item->status > Page::statusUnpublished) { $items->remove($item); } else { $item->occurrences = $count_values[$item->id]; } } $event->return = $items; }); The following is an example of how you would get the category pages that are selected in your category field. The number of times each category is selected is available in the custom "occurrences" property if you need it. // Get the selected category pages $selected_categories = $fields->categories->getSelectedPages(); // Demo output foreach($selected_categories as $selected_category) { echo "<p>Category {$selected_category->title} is selected in {$selected_category->occurrences} page(s).</p>"; } 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted May 6, 2019 Share Posted May 6, 2019 There are a few solutions and the core in the end even added an selector for the usecase. 4 Link to comment Share on other sites More sharing options...
mel47 Posted May 7, 2019 Author Share Posted May 7, 2019 Thanks everyone, but especially @LostKobrakai. This "owner" selector is wonderful! So for the sake of people searching on forum, it give this so simple selector: $CategUsed = $pages->find('categ.owner.template=activity'); Mel 2 1 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