Skip Navigation

[Resolved] get_search_query() function returns empty string for the View results

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

Problem:

The get_search_query() function was returning an empty string when used inside a Toolset View, causing the Relevanssi relevanssi_didyoumean() function to fail. The issue occurred because Toolset custom search does not use WordPress’s default ?s= parameter, so get_search_query() had no value to read.

Solution:

Instead of using get_search_query(), the Relevanssi function was modified to use the Toolset View’s search URL parameter. By passing the View’s actual search parameter value to relevanssi_didyoumean(), the “Did you mean” functionality worked correctly inside the View’s “No items found” section.

Relevant Documentation:

https://toolset.com/course-lesson/searching-texts-in-custom-fields-with-toolset-and-relevanssi/

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.

This topic contains 1 reply, has 1 voice.

Last updated by cassianS 1 day, 9 hours ago.

Assisted by: Christopher Amirian.

Author
Posts
#2846832
image (1).png
image.png

1) Tell us what you are trying to do?
- I am trying to use Relevanssi "did you mean" shortcode in the view when no results are found.
However, it didn't work, which made me contact Relevanssi support. We found that while Relevanssi relevanssi_didyoumean() function exists the get_search_query() returns nothing.

That's where Relevanssi support suggested to check with you guys whether you have any advice and/or ideas to make things work.

Here is the shortcode returing empty string

add_shortcode( 'rlv_didyoumean', function() {
$didyoumean = '';
if ( function_exists( 'relevanssi_didyoumean' ) ) {
$didyoumean = "<p>Search query is "" . get_search_query( false ) . ""</p>";
$didyoumean .= relevanssi_didyoumean(
get_search_query( false ),
'<p>Did you mean: ',
'</p>',
5,
false
);
}
return $didyoumean;
} );

2) Is there any documentation that you are following?
- regular documentation to register and use the shortcode within the Toolset view.

3) Is there a similar example that we can see?
- I couldn't find any

4) What is the link to your site?
hidden link

#2846948

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. From what I understand, get_search_query() is empty because it only reads the WordPress main search (?s=). A Toolset View Search (custom search filter) does not set ?s=, so get_search_query() can be an empty string even when the View search is working.

I found out about the fact above from:
https://toolset.com/course-lesson/searching-texts-in-custom-fields-with-toolset-and-relevanssi/

Maybe if you can identify the view search URL parameter, you can use that instead of the normal WordPress search.

Submit the View search once and check the URL. Toolset sends View filters (including text search) as URL parameters. You need the exact parameter name used by your View.

Toolset provides shortcodes to read URL parameters / search terms inside Views, for example:

[wpv-search-term param="..."] (reads the View search term from the URL param you specify)

[wpv-param name="..."] (reads any URL param)

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term

So you can replace the get_search_query(false) calls with the View’s URL parameter value, then pass that to relevanssi_didyoumean().

https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-param

Place your “did you mean” output inside the View’s No items found area. [wpv-no-items-found] ... [/wpv-no-items-found]

I will not be able to give you an exact code but I tried to give you info what can be done in this context.

Thanks.

#2848512

Thank you. I was able to edit Relevanssi to use Toolset search parameter instead