Leftfield Posted May 15, 2024 Posted May 15, 2024 (edited) Hi @All I am checking if the post pages have no links to the service or home pages. If they don't, list them. All I can get is a list of all the post pages with links. I've tried to rewrite this function like 100 times but no luck. What am I doing wrong? // Get all relevant pages directly in one query $relevantPages = $pages->find("template=post|service|home"); $blogPosts = $relevantPages->find("template=post"); $mainPages = $relevantPages->find("template=home|service"); // Function to check if a post DOES NOT link to ANY service OR home page function postHasNoLinksToMainPages($postPage, $mainPages) { $mainPagesIds = $mainPages->explode('id'); foreach ($postPage->links() as $link) { if (in_array($link->id, $mainPagesIds)) { return false; // Found a link to a main page, so return false immediately } } return true; // No links to PUBLISHED main pages found, so return true } // echo results foreach ($blogPosts as $post) { if (postHasNoLinksToMainPages($post, $mainPages)) { echo "<tr>"; echo "<td><a href=\"{$post->url}\">{$post->title}</a></td>"; echo "<td><a href=\"{$post->editUrl}\">Edit</a></td>"; echo "</tr>"; } } Edited May 15, 2024 by Leftfield
poljpocket Posted May 15, 2024 Posted May 15, 2024 (edited) You should make sure that the links you are looking for actually apply to the restrictions of the links() function in $page. The docs state that this, quote: Quote Applies only to Textarea fields with “html” content-type and link abstraction enabled. Does this shed some light? Also, and most important: I think you are using it the wrong way around. The function docs headline is "Return pages linking to this one (in Textarea/HTML fields)". Now, you want pages with no outgoing links to some other pages whereas as I understand it, the links() function gives you incoming links from other pages. So to achieve your goal, you should make a set of pages by calling the links() function on your $relevantPages and check if your post page is in the set. Edited May 15, 2024 by poljpocket add function interpretation details 2
Leftfield Posted May 15, 2024 Author Posted May 15, 2024 Hey :). Thanks for helping me!!! Nope. I got the links the same way. Made pie chart, list of all links with number of links pointed to them. But for some reason I can't get pages which do not link other pages (with different template).
poljpocket Posted May 15, 2024 Posted May 15, 2024 I have added to my post above. Maybe this is the reason?
Leftfield Posted May 15, 2024 Author Posted May 15, 2024 11 minutes ago, poljpocket said: ou want pages with no outgoing links to some other pages whereas as I understand it, the links() function gives you incoming links from other pages. I want to list all pages from template=post that don't link to template=home|service.
poljpocket Posted May 15, 2024 Posted May 15, 2024 (edited) Then your function is the wrong way around. You are looking for outgoing links but the function gives you incoming links. Try (untested): <?php namespace ProcessWire; // Get all relevant pages directly in one query $relevantPages = $pages->find("template=post|service|home"); $blogPosts = $relevantPages->find("template=post"); $mainPages = $relevantPages->find("template=home|service"); /** @var PageArray $mainPagesLinks */ $mainPagesLinks = $wire->wire(new PageArray()); foreach ($mainPages as $mainPage) { // this will remove duplicates by default $mainPagesLinks->append($mainPage->links("template=post")); } // echo results foreach ($blogPosts as $post) { if (!$mainPagesLinks->has($post)) { echo "<tr>"; echo "<td><a href=\"{$post->url}\">{$post->title}</a></td>"; echo "<td><a href=\"{$post->editUrl}\">Edit</a></td>"; echo "</tr>"; } } Edited May 15, 2024 by poljpocket 1 1
Leftfield Posted May 15, 2024 Author Posted May 15, 2024 (edited) Working like a charm. Thanks @poljpocket!!!! PS: I will post my code now on the other thread so everyone can grab Edited May 15, 2024 by Leftfield 1
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