CMS Development

RikinShah
Miembro

Site Search for blogs

resolver

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.

 

Is there any alternative way to implement it?

1 Soluciones aceptada
John
Solución
Colaborador líder | Partner nivel Platinum
Colaborador líder | Partner nivel Platinum

Site Search for blogs

resolver

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.

Ver la solución en mensaje original publicado

0 Me gusta
9 Respuestas 9
John
Solución
Colaborador líder | Partner nivel Platinum
Colaborador líder | Partner nivel Platinum

Site Search for blogs

resolver

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.

0 Me gusta
clioi
Colaborador líder

Site Search for blogs

resolver

That would be a great feature - extended to the website itself as well!

0 Me gusta
mgrubbs
Participante

Site Search for blogs

resolver

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.

 

Try something like this on your homepage:

<form action="/your-blog">
    <input type="text" name="search" default="Search" required>
    <input type="submit" value="Submit">
</form>

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.

 

Happy HubLing,

- Mike

gudevsmartagent
Miembro

Site Search for blogs

resolver

@mgrubbs 

 

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.

 

Thanks,

0 Me gusta
John
Colaborador líder | Partner nivel Platinum
Colaborador líder | Partner nivel Platinum

Site Search for blogs

resolver

This is great. With a few tweaks, I got it to work on a listing template page:

{% set querys = request.query_dict %}
{% if querys.search %} {% set search_value = querys.search|lower %}{% else %}{% set search_value = " " %}{% endif %}
{% for content in contents %}
    {% set content_searched = content.name|lower %}
    {% if content_searched is string_containing search_value %}
        {{content.name}}
    {% endif %}
{% endfor %}

content.name|lower had to be stored in a variable to work in the if statement 



I like kudos almost as much as cake – a close second.

0 Me gusta
Shaykoo
Participante

Site Search for blogs

resolver

Where do we put this HubL code in the blog template?

tstrack
Colaborador | Partner nivel Platinum
Colaborador | Partner nivel Platinum

Site Search for blogs

resolver

Looks awesome mgrubbs. Do you have a working example of this functionality?

0 Me gusta
mgrubbs
Participante

Site Search for blogs

resolver

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.

0 Me gusta
caustin
Miembro | Partner nivel Diamond
Miembro | Partner nivel Diamond

Site Search for blogs

resolver

Hey Rikin, 

 

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! 

 

- Christine

0 Me gusta