Jump to content
n0sleeves

How to use find() on child-array?

Recommended Posts

I am having trouble outputting my tagging system correctly. Each music track has a Multi-Page-List-Select field-type which tags associated categories.

Tags is a parent page with nested categories beneath. Genre, Mood, Drum Tags each contain additional categories which are ultimately the tags I am associating on the music tracks. 

Screenshot-01162015-115618AM.jpg

 In the code below. $genres is where I am having trouble.  I think my selector is correct, but my chaining is wrong? 

 
// Find Songs
$tracks = $pages->get("/Music/")->find("template=track, parent.title!=Drums");

  foreach ($tracks as $track) {
      $track_html  = "<li>";
      $track_html .= "<a>{$track->title} </a>";
      
      // FIND GENRES EACH TRACK BELONGS TO. 
      $genres = $track->tags->find("parent=genre");
      
      // OUTPUT ONLY GENRES 
      foreach ($genres as $genre) {
        $track_html .= $genre->title . " "; 
      }
      
      $track_html .="</li>";
      echo $track_html;
  }

 

Thank you 

Share this post


Link to post
Share on other sites

Interesting approach to tagging :)

You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes).

Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name:

$genres = $track->tags->find("parent.name=genre");

I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes.

Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors.

  • Like 1

Share this post


Link to post
Share on other sites

You already have the subpages from the pagefield, so the selector must be has_parent. I don't know if it works with the name / url, but for sure with the id of the parent page. 

Edit: No need for has_parent. Was a little to eager in posting. 

Share this post


Link to post
Share on other sites

You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes).

Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name:

$genres = $track->tags->find("parent.name=genre");

I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes.

Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors.

Providing the full parent path did not work. However, providing either  "parent=ID" or "parent.name=genre" did the trick! :)   BTW, great tip / idea on using ID's in case I ever need to change path or names. 

Thank you so much for the solution! 

Share this post


Link to post
Share on other sites

Not directly related to your question, but if your tag list is getting too long, it might be useful to activate the "Page Auto Complete" module from Core, which allows you to type in the tags that you want to add, instead of clicking through the list. I find this much easier when I have long lists of tags.

Using this module, an approach that I often use for tagging is the following:

  • Create a "tag" template with only a title field
  • Create a "Tags" page under which you will put all the tags
  • Create a "tags" Page field with the following options:
    • Multiple pages allowed
    • Parent of selectable pages: Select the "Tags" page created previously
    • Template of selectable pages: Select the "tag" template
    • Input field type: Select "PageAutoComplete"
    • Check the "Allow new pages to be created from field" option

Now, when entering tags, you can type in the name, if it exists already you an select it, otherwise press "Enter" and the tag will be created automatically.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...