Forum

Thread tagged as: Question

How do I fetch a multiple search string argument

With search.php, how can you show results from multiple queries in the string?

e.g. /search.php?q=foo successfully shows results for "foo" But how would I show results for /search.php?q=foo&bar so that results were returned for "foo" or "bar"?

In the default search.php I can see you can fetch the URL value to perform a basic search (as below) but I'm wondering how to do this for multiple search terms $query = perch_get('q'); // 'q' query string argument e.g. search.php?q=apples

Jay George

Jay George 2 points

  • 4 years ago

Jay. ?q=foo&bar translates to:

perch_get('q') = foo

perch_get('bar') = null

But ?q=foo&bar=hello translates to:

perch_get('q') = foo

perch_get('bar') = hello

Thanks Robert,

I understand it more now. I'm still not sure how to fetch multiple queries though.

Let's change the example slightly to illustrate what I'm looking to do…

Let's say I'm looking at musician profiles, and they have categories associated with them. One category set is "age" and another category set is "genre". Multiple genres can be picked for the genre set.

How would I fetch the age categories and genre categories from the URL?

/search.php?age=20s&genre=rap&genre=rock&genre=modern

I have found a very similar question in which the answer is to use PHP to fetch an array — https://forum.grabaperch.com/forum/04-19-2016-getting-multiple-values-for-the-same-parameter So, I think I may have found my solution! However, I'm just following up in case I've missed something simple. If not then it looks like perch_get is limited to a single value and I need to use PHP to get an array.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What you're doing there is filtering, not searching. See the section "Filtering by multiple fields" here: https://docs.grabaperch.com/functions/content/perch-content-custom/

Ok thanks, but RE getting the URL value is my assumption correct?

Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_get() is only a convenience function that checks if an option is set before retrieving it. It basically does this:

if (isset($_GET[$x]) {
    return $_GET[$x];
} else {
    return $y;
}

So you really don't need to use it at all. It's just there to make life easier.