Skip Navigation

[Résolu] Including a search bar on the search results (default archive) page

This support ticket is created Il y a 2 années et 10 mois. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par Waqar Il y a 2 années et 10 mois.

Assisté par: Waqar.

Auteur
Publications
#2275549

Hi there,

I use Toolset for my Search Results Archive page.

I then have some filters where the user can narrow down their results.

The initial search uses the standard 's' WordPress parameter.
The Toolset search uses the 'wpv_post_search' parameter.

However, how can I add a text search that overwrites the initial search, so that the user can change their search term?

Thank you.

#2275557

Ah, hold up. Perhaps the easier way to do this is to put in a generic WP search bar up top, with filters beneath. Thus the search will reset completely?

Unless there's a more recommended way of doing this?

#2275969

Hi,

Thank you for contacting us and I'd be happy to assist.

Your observation is correct and the WordPress default text search uses the URL parameter "s" whereas the Toolset's text search uses the URL parameter "wpv_post_search".

The downside, of using the default text search on the Toolset controlled search page is that it will work independently from the other Toolset search filters.

A better approach would be to remove the default text search from the search page and use the Toolset's own search field, with the other search filters.

To copy the text search term from the URL parameter "s" to "wpv_post_search" you can use the filter "pre_get_posts":
( ref: https://developer.wordpress.org/reference/hooks/pre_get_posts/ )


function CustomSearchFilter_func($query)
{
	if ( ! is_admin() && $query->is_main_query() ) {
		if ( $query->is_search ) {
			$_GET['wpv_post_search'] = $_GET['s'];
		}
	}
	return $query;
}

add_action('pre_get_posts', 'CustomSearchFilter_func');

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

As a result, when you'll perform a search using the default WordPress search, from anywhere on the website and reach the search page, the searched term will automatically be selected in the Toolset's text search field too.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar