Jump to content

Favourite/Page reference problem


Sava
 Share

Recommended Posts

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

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

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 by Sava
Found a solution
Link to comment
Share on other sites

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

2022-02-10_094550.png.5b4a5656c1116dfe0826d4413e6af16c.png

  • Like 2
Link to comment
Share on other sites

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...