Skip Navigation

[Resolved] Want to show ONLY matching results of a View Query

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

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 7 replies, has 2 voices.

Last updated by PaulS4783 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#948980
#949147

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Paul

Minesh is on vacation so I'll handle this.

I looked through the previous thread, and set up a similar-but-slightly-different solution on my local test site which works as expected in a single language and with WPML, assuming that you are using the same View on the translations of the different pages.

Here is the code I added to my site:

function tssupp_search_exact_match( $view_args, $view_settings, $view_id ){

	if ( 45 == $view_id ) {

		if ( !isset( $view_args['s'] ) ) {
			//no search term, bork the query
			$view_args['post__in'] = '0';
		}

		// search term exists, so...
		// 1. strip spaces to force a single search term
		$search = preg_replace('/\s+/', '', $view_args['s'] );
		$view_args['s'] = $search;

		// 2. modify SQL WHERE clause to use LIKE 'search' not LIKE '%search%'
		add_filter( 'posts_search', 'tssupp_modify_sql_where', 1000, 2 );

	}

	return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_search_exact_match', 101, 3 );

function tssupp_modify_sql_where( $search, $wp_query ){

	$search = preg_replace("/{(.*)}/U", "", $search);

	return $search;
}

function tssupp_remove_modify_sql_where( $query, $view_settings, $view_id ){

	if ( 45 == $view_id ) {

		remove_filter( 'posts_search', 'tssupp_modify_sql_where', 1000, 2 );
	}

	return $query;
}
add_filter( 'wpv_filter_query_post_process', 'tssupp_remove_modify_sql_where', 10, 3 );

Note that the View ID needs editing in two places.

This code assumes that the post titles in question don't contain spaces (from your original post that seems to be the case); it doesn't matter if users include spaces in the search box, they will be stripped.

It is also case-insensitive.

Lastly, for the "error" message, this simply comes from the no-posts-found section of your View Loop Output, which can say something like "no matching results, please enter a 10 digit code".

#949382

OK. Brilliant! that works!

There is still a slight issue however with the wrong language version results showing in certain circumstances.
e.g.

Go to the homepage (English version).
hidden link
1. Search this shipment code: TRP244K477
2. It will return a page with all the necessary shipping details. So far so good.
3. Click on the language switcher (Japanese version).
4. Search the shipment code TRP244K477 again.
5. It returns English search results, whereas should be Japanese.

Any ideas on this?

#949996

FYI this problem only seems to apply to Anonymous users.
When I am logged-in I can't replicate this problem.
So could it be a Views caching issue?

#950065

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Could you check what happens when you comment out all of the above custom code?

I want to know if it is related in any way to modifying the queries, or whether it happens anyway.

#951044

OK. I removed all the custom code provided by BOTH you and Minesh.
The problem, as described, still exists.
So perhaps this is an issue with Views + WPML integration?

One curious point to note:
The problem only occurs when moving languages away from the WPML "default" language.
e.g. If we reverse the order;
1. Search on Japanese
2. Click on the "English" language switcher
3. Do another search
The results are correctly returned in English (the default lang.)

#951334

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Paul,

I've split this into another thread as it is a distinct issue, I think we can close this one and I'll follow up there.

#951356

OK. Thanks Nigel.
Looking forward to your further comments.