Custom Blog Search in Hubspot COS

Dec 14, 2016 | 4 minute read

Hubspot does not provide you with any search engine for your blogs, instead pointing you to use Google search. I have prepared a very simple solution which you can use to create good looking search without redirecting your visitors to external websites. Check out working example at netguru.co.

Its fast, quick and simple, but has two disadvantages - search is capped to 200 most popular blogposts and it looks through post titles only. More advanced solution will require some changes in the code below.

Custom template for search results

You need to create a new page for search results with custom template. Im using example.com/search as URL. You will redirect users to this page with parameter q (query), ex. example.com/search?q=hubspot.

Here is an example code for search template:

{% raw %}{# you can replace 'default' with your blog id #}
{% set contents = blog_popular_posts('default', 200) %}

{# search query #}
{% set query = request.query_dict['q'] %}

<h1>Search results for: {{ query }}</h1>
{% for content in contents %}
  {% if query|lower in content.name|lower %}
    <h2><a href="{{ content.absolute_url }}">{{ content.name }}</a></h2>
    <p>{{ content.post_summary|striptags }}</p>
  {% endif %}
{% endfor %}{% endraw %}

All available content variables are described in HubL supported variables.

Blog search form

How to redirect user to proper search results? Just create a simple HTML form which will serve a search bar:

<form action="http://example.com/search">
  <input type="search" name="q" placeholder="Search">
</form>

If you need something more sophisticated, I recommend you to dive into HubL documentation and start hacking.


Hubspot Marketing Automation Netguru