Skip Navigation

[Resolved] Showing prepopulated search results

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

Problem:
I want to create a view that shows a search results page. I want to prepopulate the search term with the content of a custom post field (via shortcode). And I want the page to load automatically the results when first loading the URL. Is it possible to achieve this with the plugin?
Solution:
I assume we are talking about the search box outputted from Views shortcode [wpv-filter-search-box].

It needs some custom codes, for example, when user first loading the URL, you can use Views filter hook wpv_filter "wpv_filter_query" to trigger a custom PHP function, in this PHP function, prepopulate the search term and the search result.

For example, you can add below codes into your theme/functions.php:

add_filter('wpv_filter_query', 'my_func', 1, 3);
function my_func($query_args, $view_settings, $view_id){
    if($view_id == 123){
        if(!isset($_GET['wpv_post_search'])){
            $_GET['wpv_post_search'] = 'my search term';
        }
    }
    return $query_args;
}

Please replace 123 with your Views's ID.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 7 years 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.

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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#587809

Tell us what you are trying to do?
I want to create a view that shows a search results page. I want to prepopulate the search term with the content of a custom post field (via shortcode). And I want the page to load automatically the results when first loading the URL. Is it possible to achieve this with the plugin?

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?

#587886

Dear Antonio,

I assume we are talking about the search box outputted from Views shortcode [wpv-filter-search-box].

It needs some custom codes, for example, when user first loading the URL, you can use Views filter hook wpv_filter "wpv_filter_query" to trigger a custom PHP function, in this PHP function, prepopulate the search term and the search result.

For example, you can add below codes into your theme/functions.php:

add_filter('wpv_filter_query', 'my_func', 1, 3);
function my_func($query_args, $view_settings, $view_id){
	if($view_id == 123){
		if(!isset($_GET['wpv_post_search'])){
			$_GET['wpv_post_search'] = 'my search term';
		}
	}
	return $query_args;
}

Please replace 123 with your Views's ID.

More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

#588718

Thanks very much Luo.

One more question: How can I make variable 'my search term' in the function, for instance for the content of a Custom Post Type?

Let's say I have a DB of restaurants. When showing the information of one of them in a post, I would like to show a list of other restaurants in the same zip code, being ZC the content of a Custom Post Field.

#588997

It will need other custom codes, I suggest you create new thread for it, describe detail steps for the your question:
How can I make variable 'my search term' in the function, for instance for the content of a Custom Post Type?

#589260

Thanks very much again Luo.

#589298

You are welcome