Jump to content

Find pages by multiple values (page field)


MuchDev
 Share

Recommended Posts

Hello there. 
I'm trying to write a seemingly simple function and am a bit confused as to why this isn't working. I have a page full of items that have tags in their own page fields. I add those to an array and then implode them to a select string. I then would like to find pages that are tagged with those tags. For instance some items are tagged with a tag that is used to build an entire page. That page has a field called TagSelector. Some times a tag will contain some items that have pages built from that tag and I want to have some buttons to allow the user to just click on through to a larger section. So the issue is I will have to do a search for many terms at once to find a page that is tagged. When I run the search I am unable to locate anything for some reason. Will this require that I run searches separately or is there something simpler that I am missing?

This is what is passed to the find : TagSelector=10734|10715|8192|10711|13311|10712

function renderTagButtons($q,$pageItems){
	if($q !== "" || $q !== null){
	
		$tagnames = "";
		$tags = new pageArray();
				
		foreach($pageItems as $pageItem){
			$tags->append($pageItem->Tags);
		}
		
		$tags = $tags->find("sort=title");	
		
		foreach($tags as $tag){
			$tagnames[] = $tag->id;
		} 
		
		$alltags = implode("|",$tagnames);
		$alltags = 'TagSelector='.$alltags;
		echo $alltags;
		
		$matches = wire('pages')->find("$alltags");
		
		if(count($matches)){
			$tagBtns .= '<h3>Related pages:</h3>';
			
			foreach($matches as $match){
				$tagBtns .= "<a href='{$match->url}' class='btn btn-default sectionbtn'>{$match->title}</a>";
			}
			return $tagBtns;			
		}
	}
}

Link to comment
Share on other sites

You define $tagnames as an empty string with

$tagnames = "";

Later in your foreach loop you treat $tagnames as an array and add items to it in

$tagnames[] = $tag->id;

Maybe you should declare $tagnames as empty array in the first place, like

$tagnames = array();

not sure though if this solves your problem...

EDIT: Sorry, I just read in the PHP documentation

$arr[key] = value;
$arr[] = value;
// key may be an integer or string
// value may be any value of any type

If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array

So forget about what I said above...

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