Jump to content

Working with tags

Go to solution Solved by diogo,

Recommended Posts


I'm created a site where it will have related case studies depending on the tags. I've got it working and it works perfectly, but I was just wondering if there was a more elegant approach using more of ProcessWires powerful API. 

So I have a page called Tags that are hidden, with the children being the tags themselves. On each Case Study I have a Tag field that is selectable from the Tags page. 

Then in my code I have:

foreach($page->tags as $tag) {
     $a[] = $tag->title; 
$a = implode("|", $q);
$results = $pages->find("tags~=$a")->shuffle();
foreach($results as $result) {
     if($page->id != $result->id) {
         echo "<a href='$result->url'><h1>$result->title</h1></a>";

Let me know if there is a more elegant way of doing this. I'm obsessed with clean code :(



Link to post
Share on other sites

Looks fine to me. If you are obsessed with clean code you should read up on the api, there is tons of stuff that will save you some lines. For instance your couple lines could be blitzed with one little line of code. 


//no need to loop the results to an array if that is what you want

$query = $page->tags->implode("|","id");
$results = $pages->find("tags=$query")->shuffle(); 
Link to post
Share on other sites
  • Solution

Also, you can remove the current page from the array to avoid the IF check inside the loop

$results = $pages->find("tags={$page->tags}")->remove($page)->shuffle();

Or, do the same as above with one less method:

$results = $pages->find("id!={$page},tags={$page->tags}")->shuffle();
Edited by diogo
added second method
  • Like 6
Link to post
Share on other sites

Diogo, you are amazing! Thank you very much. ProcessWire is so beautiful. Remove doesn't seem to be on the Cheatsheet. There has been quite a few times people have suggested using things, that aren't on the Cheatsheet. I would love to see the Cheatsheet updated eventually. 

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

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.

      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
    • By Andi
      Continuing my journey into PW hooks, I'm trying to find a way to retrieve all images from a page that explicitly *do not* have a certain tag (or tags) attached to them.
      Found this post from 2015
      But I'm wondering if there's a more elegant way to go about this.
      Let's say I have a multi-image field called "images_header" and instead of
      $page->images_header->findTag('mytag'); I would like to do this:
      $page->images_header->excludeTag('mytag'); So I'd be able to do
      // find images that don't have the tag "mytag" $images = $page->images_header->excludeTag('mytag'); // check if there's any images if (count($images)>0) { // do something.. } Would this be possible by hooking into Pagefiles somehow?
      There's this bit in /wire/core/Pagefiles.php Line 626 that I'd basically just need to reverse (or at least in my mind 😄 )
      public function findTag($tag) { $items = $this->makeNew(); foreach($this as $pagefile) { if($pagefile->hasTag($tag)) $items->add($pagefile); } return $items; } Any ideas on how this could be done in a graceful manner?
      Thanks in advance!
    • By Sanyaissues
      After enable the Tags File Compiler module, echoing variables with brackets syntax doesn't work. I try something simple like {page.title} and is displayed like the tag isn't parsed:
      {page.title} This problem happens with PW 3.0.108, i have another installation with 3.0.82 and it works. Any Ideas? thanks.
    • By sww
      Hey there,
      is there really no way to turn the OR logic into an AND logic when selecting pages by (e.g.) tags?
      so instead of
       $pages->find("template=exhibitions, tags=foo|bar") something like
       $pages->find("template=exhibitions, tags=foo&&bar") so the pages needs to have all requested tags, not just any of them.
    • By dimitrios
      I have created a field of type Page Reference and input field type Page Auto Complete, so that users of role 'writer' can add new tags to their articles. However, only a superuser can add new tags through the field, even though 'writer' roles have the permission to create  pages of template 'tag', and the permission to add children in the parent template. New tags in the Page Tree can be added normally. Is there something I am missing?

  • Create New...