Jump to content

Recommended Posts

Posted (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 by Leftfield
Posted (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 by poljpocket
add function interpretation details
  • Like 2
Posted

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

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

Posted (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 by poljpocket
  • Like 1
  • Thanks 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...