Hide Blog Post when Search Results Display

I have search form enabled on my blog layout. I am using the {% component source: “Page”, layout: “Site Search List”, type: “site_search”, object:“collection” %} to display the search results on the page which works great.

But when search results are displayed I want the blog normal posts to be hidden on the page instead of listed still underneath. Problem is that if I use {% if searchResults == ‘’ %} or {% if searchResults.items %} it seems to hide the posts all the time. Anyone know a work around for this?

Try: if searchresults == null
Haven’t tested this as away from my computer, but should work.

You may need to add collectionVariable: "searchResults" to your search component tag so that a collection is made under that variable - in which you are checking against.

Alternatively, check for the URL parameter which should be added by the search function, eg:

{% if request.request_url.params.SearchKeyword == null %}
    show blog
{% endif %}

Checking the url parameter does it perfectly, thanks @Adam.Wilson!!!

Do you know if there is a way to only search Blog Posts instead of all pages (other than switching off all the other assets throughout the website)?

You may be able to add ‘filterBy: "parentId, filterValue: "your blog id’.
If not, in the searchResults collection have a liquid condition like ‘if parentId == your blog id’ should work by only showing those blog post.

Hmmm can’t seem to make it happen with either @Adam :frowning:

Ok, back at my computer so can do some tests…
So you’ll need to suppress the normal layout (or create a new one) and add a collection for the results. Then set up a condition in your forloop:

{% component source: "Page", layout: "", type: "site_search", collectionVariable: "searchResults" %}

<ul>
{% for sr in searchResults.items %}
{% if sr.parentId == 1234 %}
    <li><a href="{{sr.url}}">{{sr.name}}</a></li>
{% endif %}
{% endfor %}
</ul>

Where 1234 is you Blog group ID.

NOTE: this is not ideal for search results as there may still be other items returned in the results (just not shown), so if you have pagination enabled, for example, the paging would not be accurate.

Actually, just been hacking the Custom Module search and have got it working for Blogs, so you can actually do this:

<form>
	<input type="hidden" name="prop_ModuleId" value="1534" >
	<input type="hidden" name="prop_ParentId" value="1234" >
	<label>Keywords</label>
	<input type="text" name="prop_KeyWords" maxlength="255" value="{{request.request_url.params.prop_KeyWords}}" >
	<input type="submit" value="Search" >
</form>
{% component source: "Blog Post", layout: "List", isSearchResult: "true", type: "module" %}

Where the prop_ParentId value is your Blog group ID (if you want to search across all blogs leave this blank).

And since your search results are on the same page as your posts you could do this to show/hide appropriately :

{% if request.request_url.params.prop_ModuleId == null %}
    --YOUR CURRENT BLOG COMPONENT TAG--
{% else %}
    {% component source: "Blog Post", layout: "List", isSearchResult: "true", type: "module" %}
{% endif %}

Hmmm, still doesn’t seem to want to work for me. I will have a bit more of a play with it.