Sava Posted February 8, 2022 Share Posted February 8, 2022 Hi, I am bit stuck and would really appreciate your help. I created a page reference filed for "user" template called "favourite" which stores the favourite user's "post". There is no problem while i try to loop it like foreach($user->favorite as $item) { echo "<li><a href='$item->url'>$item->title</a></li>"; } In "post" template there is a page reference filed "category". Now here is the part where I get stuck. I am trying to display user favourite posts in that category Ex: in category A user has no favourites. in category B user has 4 favourites Hope you can understand my problem and help me, thank you ? Link to comment Share on other sites More sharing options...
Robin S Posted February 9, 2022 Share Posted February 9, 2022 This assumes that the "category" field allows for selection of a single page: $results = []; foreach($user->favorite as $item) { $category = $item->category->title ?: 'Uncategorised'; $results[$category][] = $item; } foreach($results as $category => $items) { echo "<h3>$category</h3>"; echo "<ul>"; foreach($items as $item) { echo "<li><a href='$item->url'>$item->title</a></li>"; } echo "</ul>"; } Link to comment Share on other sites More sharing options...
Sava Posted February 9, 2022 Author Share Posted February 9, 2022 (edited) Thanks for the help Robin, yesterday I figured I can use "find". But only thing that is a problem now I can't get title or id for page refrence $catFav = $user->favorites->find("template=blog-single, cat_single=$page->path"); Ex. When I try to use one category id, it works. $catFav = $user->favorites->find("template=blog-single, cat_single=(category id)"); But when I try something like $page->path or title to get them all working, it just doesn't work EDIT: I used $page->id in cat_single and it worked ? Edited February 9, 2022 by Sava Found a solution Link to comment Share on other sites More sharing options...
Robin S Posted February 9, 2022 Share Posted February 9, 2022 Not sure I understand the question, but if you have a Page object (e.g. $page, $my_page) and you want to match it to a Page Reference field in a selector string the best way is to match by the page's ID. my_page_reference_field=$page->id Or you can actually just do... my_page_reference_field=$page ...because a Page object becomes an ID when it is treated as a string. Similarly a PageArray will become a pipe-separated string of IDs that can also be used directly in a selector string: my_page_reference_field=$my_page_array 2 Link to comment Share on other sites More sharing options...
Sava Posted February 9, 2022 Author Share Posted February 9, 2022 Thank you @Robin S $page->id was the thing I needed ? 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