Recent searches


No recent searches

Default search to show articles only

Answered


Posted Jun 14, 2022

I need to be able to change the default setting in Guide search so that it shows articles only, rather than articles and community posts. I don't want to hide community comments completely, just change the default results. Is there a way to get this so that users can still select to include community posts using the left-hand menu?

This is the same a Jeff S' question here. Support also mentioned it may need coding, so any help with what I'd need to add would be greatly appreciated!


0

17

17 comments

image avatar

Erica Girges

Zendesk Developer Advocacy

Hi Gwyn,
 
It does require a bit of custom JS code. You'll want to take a look at the Help Center templating cookbook to get familiar with how to edit code within your Guide theme. This post also shows a bit of a demonstration on one way to achieve it. 
 
Hope this helps!
 
Erica

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Hi Gwyn Mabo :)

 

Copy and paste the given JS code to your script.js file:

  • It shows articles only, rather than articles and community posts.
  • Users can still select to include community posts using the left-hand menu.
$(document).ready(function() {
  if(templateName == "search_results") {
      if (window.location.href.indexOf("community") < 1) {
          if (window.location.href.indexOf("source") < 1) {
             $('.breadcrumbs.search-result-breadcrumbs li:nth-child(2) a:contains("Community")').parents(".search-result-list-item").hide();
        }
     } 
  } 
})




Screenshot:




Complete Guide: https://support.zendesk.com/hc/en-us/community/posts/4589496379162-How-to-exclude-Community-posts-from-Guide-search-results-

 

 

 

 

Thanks

Team

 

0


Thank you so much, Ifra! Which line would I paste this code into or does it not matter too much?

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Just copy the script code and paste it at bottom area to script.js file.

Screenshot:

 

 

 

Add template name to the search results file:

 

 

 

Copy the jquery CDN and paste it to the document_head.hbs file.

jquery CDN:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>







Screenshot:


0


image avatar

Erica Girges

Zendesk Developer Advocacy

Thanks Ifra! Appreciate you sharing your solution. 😊️

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

 😊️

0


Hi Ifra, I've applied the code as instructed, but it hasn't changed the search results format at all. Is there anything else I've missed?

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

@Gwyn Mabo, it's working for me, may be classes are different or something else. Can you share your URL of your HC?

0


Hi Ifra, I haven't edited the code on my live site, but a draft theme.

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

If you didn't add the code in your live site then how can you get any kind of output for live as you want?

You added in your draft theme then you can see the output in your draft theme search results page not in your live site.

 

0


Hi Ifra, I wasn't using the live site to test it, I was using the preview version of the draft theme. I assumed (like it does with other changes viewed using the preview) that this would work.

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Hi Gwyn Mabo

Test like this:-

Search "What" in the search-bar.

Only articles would be list with what string not community posts.

But when you remove the added code (Given above) from script file then all posts would be shown (If exist with the 'what' string) as default results on the page.

0


Hi Ifra, that is exactly what I did, and it did not work I'm afraid.

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

Hi Gwyn :),  is there any way I can see the bug in your theme?, then I can provide some solution.

0


Hi Ifra, more than happy for you to help out, what do I need to do?

0


image avatar

Ifra Saqlain

Zendesk LuminaryMost Engaged Community Member - 2022Most Engaged Community Member of The Year - 2021Community Moderator

You should set your draft mode theme live, then share the URL here.

0


Instead of suggesting that we do custom javascript is it possible zendesk make a change to the search helper?
https://developer.zendesk.com/api-reference/help_center/help-center-templates/helpers/#search-helper

providing a filter option for either : unified,  knowledge_base, or community
linking the user directly to http://<domain>/hc/en-us/search?filter_by=<chosen_filter>&query=<query>" so we can default to showing knowledge_base articles but easily provide the option for community as well. 

A workaround I found was to create the following in order to override the searches default functionality.

 const manuallySearch = (event) => {
    event?.preventDefault();
    event?.stopPropagation();
    let query = document.getElementById('query').value;
    window.location.replace(`${window.location.origin}/hc/en-us/search?query=${query}&filter_by=knowledge_base&utf8=✓`);

    return false;
};

$(() => {
    let form = document.getElementsByClassName('searchbox_classname')[0];
    form.removeAttribute('action');
    form.addEventListener('submit', manuallySearch);
    form.addEventListener('keydown', (event) => event.key === 'Enter' && manuallySearch(event));
});

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post