CMS Development

fair
Participant

Setting up search for multiple blogs on the same domain

I'm running into a peculiar issue that I am most likely just doing something very wrong, or maybe it's not possible to have two different blogs on the same domain?

 

I set up a second blog, and the search feature is working as expected, however on the first blog I set up, it will only search the second blog. The search for the first blog is not operational. When I try to search for content that I know is in the first blog, it doesn't show any results. Any results I see are only blog posts from the second blog.

 

Is there a way to set up the search pages to return results for only the blog they are currently viewing?

A nice guy on slack gave me this code, and it is appending to the URL properly but still not searching the right blog, and once you are on the search page if you run another search, then it doesn't keep this appended. Once you are on the search page it breaks as well.

 

<div class="ac-blog__action-search hs-search-field"> 
<div class="hs-search-field__bar">
<form action="/{{ site_settings.content_search_results_page_path }}">
{% if module.field_label %}
<label for="term">{{ module.field_label }}</label>
{% endif %}
<input type="text" class="hs-search-field__input" name="term" autocomplete="off" aria-label="Search" placeholder="Search">
<input type="hidden" name="type" value="BLOG_POST">
<input type="hidden" name="type" value="LISTING_PAGE">

{% for content in contents && loop.index == 1 %}
{% set my_blog = blog_by_id (module.blog) %}
<input type="hidden" name="pathPrefix" value="{{ my_blog.slug }}">
{% endfor %}
<button class="ac-button ac-button--filled-dark" aria-label="Search">
{{ module.sidebar_widget.search.button_text }}
</button>
</form>
</div>
<ul class="hs-search-field__suggestions"></ul>
</div>

 

3 Replies 3
GRajput
Recognized Expert | Gold Partner
Recognized Expert | Gold Partner

Setting up search for multiple blogs on the same domain

Hi @fair 

 

You're dealing with a common issue when trying to filter search results for multiple blogs on the same domain. By default, most search functions group all content of a certain type (like blog posts) together, without distinguishing between different blogs.

Why Your Current Code Isn't Working:  
. Hidden Input Conflict:
 You have two <input type="hidden" name="type" value="..."> elements. Since the second one overrides the first, your search likely only includes "LISTING_PAGE" content and ignores "BLOG_POST" content.
 
. Path Prefix Issue:
 Your {% for content in contents && loop.index == 1 %} loop runs only once when the blog listing page first loads. This means the pathPrefix (which helps identify the blog) is only added to the search URL at the start. Once you reach the search results page, this loop doesn’t run again, causing subsequent searches to lose the pathPrefix.
 
. Search Engine Behavior:
 Just adding pathPrefix to the URL doesn’t mean the search engine will automatically filter by it. The search system must be explicitly set up to recognize and apply it as a filter.

How to Keep Search Results Limited to the Same Blog:
To ensure pathPrefix is always included in search requests (even after navigating away from the main blog page), here are three solutions:

Option 1: Use JavaScript to Keep pathPrefix Persistent (Most Flexible)Modify your search form and use JavaScript to make sure pathPrefix is always included.
 
Step 1: Fix your search form so it correctly includes BLOG_POST, LISTING_PAGE, and pathPrefix from the start.
Step 2: Use JavaScript to check if pathPrefix is missing on the search results page and add it if needed.

Option 2: Modify the Search Results Page (Server-Side Solution)If your platform allows, update the search results page so it always includes pathPrefix.
 
Step 1: Ensure the search form on the results page includes the correct pathPrefix so it persists across searches.
Step 2: If possible, retrieve current_blog_slug from session data or check the HTTP referrer to determine which blog the user came from.

 

 

I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks!




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


0 Upvotes
fair
Participant

Setting up search for multiple blogs on the same domain

By the way, I did try removing either BLOG_POST or BLOG_LISTING, and neither of those worked. It also still doesn't solve the issue of searching from the search listing page. How can I make it to where it just searches all blogs on this domain? Because if I remove the pathPrefix it still is only searching one of the blogs.

0 Upvotes
fair
Participant

Setting up search for multiple blogs on the same domain

I think I need a little bit more help. I haven't had a chance to focus on this yet.  In the code I posted above, I think I was attempting to do your Option 1 and couldn't get it to work. I'm not sure what to change in the code.

As for Option 2, is there a file in the Design Manager that I should be editing?

I still feel like I am missing something to finish this. 

0 Upvotes