Is there any good we to implement Keyword search on Blog sites? I read post of usign Google Custom Search in past but as of April google stopped paid subscription for Google Custom Search and we dont want advertisement on our search.
Hey all, it's 2019 and now HubSpot has native site search. You can even narrow a search form's scope to blog posts.
If you want to fine tune it to only include one blog and exclude other blogs on a portal, then you will have to clone the module and modify it from there.
I like kudos almost as much as cake – a close second.
Hey all, it's 2019 and now HubSpot has native site search. You can even narrow a search form's scope to blog posts.
If you want to fine tune it to only include one blog and exclude other blogs on a portal, then you will have to clone the module and modify it from there.
I like kudos almost as much as cake – a close second.
You can use query string parameters and the "is string_containing" expression test. When creating the blog content loop. To connect a "search bar" to this search functionality, you'll need a simple form that, when submitted, refreshes the page OR takes the user to a new "search results" page.
The code above puts a simple form with a text box and a submit button wherever you want. When the user clicks "submit" (after adding a search term) they will be taken to yourdomain.com/your-blog (which should be your blog slug).
Within your Blog Listing code (take a walk through this resource to learn more), find the {% for loop %} that goes through each piece of content before outputting the HTML. It will typically look like this:
{% for content in contents %}
The magic here is in reading the values submitted by the form from the previous page. We can use the request.query_dict variable (see more about supported variables) to check for a "search" parameter in the URL.
{% set querys = request.query_dict %}
{% if querys.search %} {% set search = querys.search|lower %} {% else %}{% set search = " " %}{% endif %}
This sets a variable = to all of the querys in the url. Ex: your form submit leads you to:
The querys variable would now equal a dictionary of 'search': 'fun' -- you can now use querys.search to directly get the value "fun" for use in your comparison to the post title.
Note: I used the lower filter to make the search term all lowercase (this will be the same for the title of the post so we don't have case matching issues)
Here's what the full code for your blog listing should look like:
{% set querys = request.query_dict %}
{% if querys.search %} {% set search = querys.search|lower %}{% else %}{% set search = " " %}{% endif %}
{% for content in contents %}
{% if content.name|lower is string_containing search %}
{{content.name}}
{% endif %}
{% endfor %}
This full copypasta checks for a query_dict in the URL, sets the dictionary to the search query, loops through all blog content in your content list, and compares the search term to the title of the post.
You can use this functionality wherever you want really, as you can call blog_recent_posts(<blogid>,100) wherever (landing pages, blogs, website pages). So if you want to have the search bar on your homepage and link to a "search results" page instead of a blog, you can do that as well.
We used the same code, but if we search for titles from the first three articles shown on the page, we get results. If we search for titles from articles on the succeeding page (2), we don't get any result.
I don't have a public-facing version unfortunately - but you can very easily test it out yourself on an internal blog by adding in the HubL code (don't need the form) in the listing view of a blog and putting a ?search=word in your blog listing page when you are on the live blog url.
You are right, as of April, Google has stopped offering their GSS and now shows advertisements if you try using their custom site search. If you are looking for alternatives to try, check out some of these. One of them may be an acceptable substitute to avoid ads!