Need to Search by Whole Tag

Hi all,

I’m working on a site that is going to have tags with multiple words, such as “style guide” and “home improvement”. The problem I’m having is that I’ll need to search for articles by the entire tag - not just a single or partial word.

For instance: if I search for “home improvement”, I need to get only the articles with this tag, and not the articles tagged “personal improvement” or “home tips”.

Is this possible to do?

In this case, wouldn’t it be better to filter instead of using search?

The filterBy method allows to search by tag. The following code will only return articles that have the “home improvement” tag (where the tags field is split by ,):

$articles = page('articles')->children()->visible()->filterBy('tags', 'home improvement', ',');

@texnixe & @lukasbestle: thank you once again! Indeed, I was stuck trying to do it all with search(), when filterBy() suits the problem better!

You guys are awesome! :grin:

@lukasbestle: in the docs there doesn’t seem to be anything about the last parameter for filterBy() you used in your example… Is this an undocumented, fourth parameter? It’s very useful!

Yes, you are right, @luxlogica, it’s currently missing from the docs, but @lukasbestle has already added it.