Search Results - select Article type by default | Community
Skip to main content

Search Results - select Article type by default

  • May 10, 2023
  • 14 replies
  • 0 views

Hi all,

Is it possible to select by default the Articles type on the search results page (instead of All types)? 

We're finding in our help center that most users don't even know to select the Articles type to show the By category subfilter. We didn't even realize that function ourselves when we were building the help center. It's just not intuitive.

If Articles is selected by default, users will always see the categories.

Thanks

14 replies

Ifra
  • May 11, 2023

Hi :) 

@Rob R

 

I worked on your query and I found this:

Add it to document_head.hbs file.

<meta id="meta-location" http-equiv="refresh" />
<script>
  if(window.location.href.indexOf("/search") > -1) {
       document.querySelector('#meta-location').setAttribute('content', '0;url=https://support.zendesk.com/hc/en-us/community/posts/5683688413338-Search-Results-select-Article-type-by-default');
    }
</script>

 

Replace this URL with yours:

https://support.zendesk.com/hc/en-us/community/posts/5683688413338-Search-Results-select-Article-type-by-default


  • Author
  • May 11, 2023

Thanks for the quick reply, Ifra!

The only thing is that the page still loads to the standard results view, then one second later it refreshes to the redirect URL. I thought the '0' in the code is supposed to make it immediate?

Side note: I needed to change /search to search?utf8, since both the original results URL and the redirect both contain /search (and resulted in a page refresh loop). 


Ifra
  • May 11, 2023

O o!  I only tested for external URL instead of search results. 

I'll fix it and provide new solutoin.


  • Author
  • May 16, 2023

OK, thanks!


Kel
  • June 13, 2023

Hi, just asking politely for some help on this as I can't get the code to work (it goes in the loop as mentioned by Rob).

Is the URL that's meant to be replaced (https://support.zendesk.com/hc/en-us/community/posts/5683688413338-Search-Results-select-Article-type-by-default) supposed to be the just the domain? The link to the search page?

And with the replacement of the search?utf8, does that mean the code is this?

<meta id="meta-location" http-equiv="refresh" />
<script>
if(window.location.href.indexOf("/search?utf8") > -1) {
       document.querySelector('#meta-location').setAttribute('content', '0;url=https://support.zendesk.com/hc/en-us/community/posts/5683688413338-Search-Results-select-Article-type-by-default');
  }
</script>

Ifra
  • June 14, 2023

Hi Kel S., actually when you add external URL in the code it won't be stuck in a loop but I'm checking the search page in the condition so the page is same on every reload that's why code executes again and again.

I tried to solve it from my side but my bad :(


David139
  • July 22, 2023

Hi @ifra,
I had the same issue in the sense that I wanted the user to able to clearly understand where their search was applied - and we don't have a community yet.

I'm using the following script based on yours :)

But would there be a more elegant way to do it? Especially that doesn't involve a refresh of the page?

<meta id="meta-location" http-equiv="refresh" />
<script>
  if(window.location.href.indexOf("/search?utf8") > -1) {
  var url=window.location.href;
  var pattern= '/search?type=knowledge_base&utf8';
  newUrl= url.replace("/search?utf8",pattern)
       document.querySelector('#meta-location').setAttribute('content', '0;url='+newUrl);
    }
</script>

Ifra
  • July 23, 2023

Hi David Monteiro, I tried it to get it done but that same bug occurs, infinite page refresh loop...

The code is working for external URL but when I try for search page I stuck in same issue :)

 


Fernando29
  • September 5, 2023

@ifra, after a lot of tinkering I was able to add the script tag below at the bottom of the search_results.hbs with the following results:

  • Hide the Menu with Source and Category on the side
  • Select the Category by default
  • Show all Categories expanded
<script>
    (function() {
        // Function to hide the 'Type' section
        function hideTypeSection() {
            const typeSection = Array.from(document.querySelectorAll('.collapsible-sidebar-title.sidenav-title')).find(section => section.textContent.trim() === 'Type');
            if (typeSection) {
                typeSection.closest('.filters-in-section').style.display = 'none';
            }
        }

        // Check the last li > a element within .multibrand-filter-list
        const lastLink = document.querySelector('.multibrand-filter-list li:last-child > a');
        if (lastLink) {
            if (lastLink.classList.contains('current')) {
                // If it has class "current", hide the Type section
                hideTypeSection();
            } else {
                // If it doesn't have class "current", click it and then hide the Type section
                lastLink.click();
                hideTypeSection();
            }
        }

        // Observe changes in the sidebar for any subsequent updates
        const targetNode = document.querySelector('.search-results-sidebar');
        if (targetNode) {
            const config = { attributes: true, childList: true, subtree: true };

            const callback = function(mutationsList, observer) {
                for(let mutation of mutationsList) {
                    if (mutation.type === 'childList') {
                        if (lastLink.classList.contains('current')) {
                            hideTypeSection();
                        } else {
                            lastLink.click();
                            hideTypeSection();
                        }
                    }
                }
            };

            const observer = new MutationObserver(callback);
            observer.observe(targetNode, config);
        }
    })();
  </script>

 


Ifra
  • September 6, 2023

Mr. Fernando, it's really nice trick and it will help to other members. Thank you so much that you shared it here and I learned it too :)


Rasmus17
  • October 25, 2023

Another approach would be to let the form build the correct query string when submitting it, simply add a hidden input by adding this to script.js

const search_form = document.querySelector('form.search');
const input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', 'type');
input.setAttribute('value', 'knowledge_base');
search_form.appendChild(input);

Ifra
  • October 26, 2023

Hi Mr. Rasmus Hedback :),

Thank you for the nice tip.


  • May 31, 2024

Thank you @fernando29
This was exactly what I was looking for! Kudos to you ^_^


Using API v3 none of the suggestions work, unfortunately. Does anyone have any advice for an updated code snippet to hide the Type menu and default search results to Articles, allowing Categories to be visible?