Jump to content

Returning products from a GET input with an AND selector...


a-ok
 Share

Recommended Posts

Apologies if this has been asked before but couldn't find anything...

I have a simple tag system set up, which uses an overview template. At the top of my template is as follows:

if ($input->get("tags")) { // If GET ?tags= exists...

	$currentTags = $input->get("tags");
	$currentTags = $sanitizer->text($currentTags);

	$products->filter("stock_detail_tags.name=$currentTags"); // Filter products by the tags
	$pageTitle = str_replace('|', ' > ', $currentTags);

	if (!count($products)) throw new Wire404Exception(); // If no returned data then return 404

}

The issue is my URL for if someone selects a furniture tag is /stock/?tags=furniture|mirrors which will return pages that have either the furniture or mirrors tag. What I need to do is amend this so it'll only return pages that have both furniture and mirrors tags.

I understand what I need to do (use the AND selector) but I'm a little unsure in this setup whether I should be changing the URL or the code?

Link to comment
Share on other sites

Hello,

can you try this:

if ($input->get("tags")) { // If GET ?tags= exists...

	$currentTags = $input->get("tags");
	$currentTags = $sanitizer->text($currentTags);
    
	// tags=furniture|mirrors
	$selector = "stock_detail_tags.name=" . str_replace('|', ',stock_detail_tags.name=', $currentTags);
	
	$products->filter("$selector"); // Filter products by the tags
	$pageTitle = str_replace('|', ' > ', $currentTags);

	if (!count($products)) throw new Wire404Exception(); // If no returned data then return 404
}

To get AND inside query, you need to repeat query parameter (target) inside selector (example: 'tags=foo, tags=bar').

Regards.

  • Like 1
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...