Skip Navigation

[Resolved] Search text on a single page and show result

This thread is resolved. Here is a description of the problem and solution.

Problem: I have created a page with Divi Builder, and I would like to include a search form where people can type text and see if the content exists on the page.

Solution: Create a custom search View of Pages, filtered by Page ID. Insert the Page's ID here, so the View only searches text in the current Page. In the Filter Editor panel, insert a text search and submit button.

In the Loop Output editor, include different messages depending on whether or not the User's text is found in the current page, and wrap the success message in a span with a unique CSS class:

[wpv-layout-start]
    [wpv-items-found]
        <span class="js-hide-on-load">We've been there.</span>
    <!-- wpv-loop-start -->
        <wpv-loop>
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        We haven't been there.
    [/wpv-no-items-found]
[wpv-layout-end]

Add some JavaScript to the Loop Output JS panel to hide the message when the page first loads:

jQuery(document).ready(function(){
  jQuery('.js-hide-on-load').hide();
});

Relevant Documentation:
https://toolset.com/documentation/user-guides/digging-into-view-outputs/
https://toolset.com/documentation/user-guides/front-page-filters/

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by matthewh-6 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#616443

I am trying to: I want to be able to search the text on the page and for it to say "Yes, we have been there." or if it doesn't fin the text on the page, it says, "We have not been there."

Link to a page where the issue can be seen: hidden link

I expected to see:

Instead, I got:

#616542

Hi, can you tell me how each location is stored on your site? Is each location a custom post, or a custom field associated with a post, or are the locations simply part of a single post's main content, or is there some other way you have set up this list of locations?

Views has a text search filter that will help you search for text in a post's title or a post's body text. If you need to include taxonomy terms and custom field values in that search, you need to use a plugin like Relevanssi to extend the text search functionality. More information about that here:
https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/

If the search text is found in any post, then you can show the "We've been there" message. If not, you can show the "We have not been there" message. You can do this in the Loop Output of a View of posts. Just leave the wpv-loop tags empty, and place your message outside the loop and in the "no results found" area like this:

[wpv-layout-start]
	[wpv-items-found]
        We've been there.
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		We haven't been there.
	[/wpv-no-items-found]
[wpv-layout-end]

Let me know how these locations are set up on your site and I can offer some more advice.

#616568

Hi, thanks for the reply.
I did not use types or any custom post types to make that page.
It was made with Divi's toggle module.
They are just plain text inside the toggle on the page.

So then I should use the Relevanssi plugin to help achieve this effect?

Thanks.

#616585

If the toggle modules were added to a page or post using the Divi builder, then Relevanssi will not be necessary. I just ran a quick test and I was able to get search working without it. I created a View of Pages (since I added the toggle modules to a page), and added a Query Filter to filter by post ID. I inserted the Page's ID there, so the View only searches through the content of this one Page.

Then I went to the Filter Editor panel. If you cannot see the Filter Editor panel, activate it in the Screen Options tab at the top of the View editor admin page. Here I added a Text Search and a Submit button. Next I placed the code from above in the Loop Output editor.

Then in the Page editor, I inserted this View into a Text module using the Fields and Views button. Now when I enter text into the search box and click Submit, I see the results filtered as expected. One minor thing here is that the text "We've been there" shows in the results even before text is entered in the search box. If that's a problem for you, we can work on modifying that once you have the View working up to this point.

Let me know if you are able to get this working, or if you get stuck somewhere.

#616605

Nice! Thanks.
Got it to work.
Yes, the text "We've been there" shows in the results even before text is entered in the search box.
How would I make that hidden until they hit the submit button?

#617159

Okay great. Make a small adjustment in the Loop Output editor that wraps the message in a span tag, then add some JS to hide this span on initial page load. Loop editor:

<span class="js-hide-on-load">We've been there.</span>

JS panel:

jQuery(document).ready(function(){
  jQuery('.js-hide-on-load').hide();
});
#617906

Awesome. Thanks!

One last question.

Is there any way to set an anchor? Like if they type in the word that is on the page, it then jumps to it and open the toggle so the person can see it.

Thanks!
Matt

#618065

I don't know if Divi offers the ability to programmatically open a toggle module with JavaScript. The documentation here does not mention it:
hidden link

Are you able to provide a link to any documentation showing how to do this part? It's not really Toolset-related so I'm not able to research this for you.

#618488

Ok Thanks for all your help.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.