Skip Navigation

[Resolved] Conditional search how to

This support ticket is created 6 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/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Kostas 6 years ago.

Assisted by: Waqar.

Author
Posts
#1143228

Hi,

The search at: hidden link
searches in wpcf-variety if wpcf-blend = 0.

I need to search in (wpcf-variety-member1 OR wpcf-variety-member2 OR ... OR wpcf-variety-member7) if wpcf-blend = 1.

I need some guidance here.
I can give admin access, currently the site is also p/w protected.

Thank you in advance,
Kostas

#1143740

Hi Kostas,

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

To better understand how the search filters are set, I'll need access to the page itself.

Can you please share temporary admin and front-end access to your site, so that I can log in and see how the page is set up?
( preferably to a test/development site, if possible )

Your next answer will be private which means only you and our support team will have access to it.

Note: Please backup your database and website before sharing the access information.

regards,
Waqar

#1143788

Hi Kostas,

Thank you for sharing the access details.

To change the query of your view, based on the value(s) selected in the search filter, you can use the filter "wpv_filter_query":
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

For example, for your view with ID "112", you can add the following code in your active theme's "functions.php" file:


add_filter( 'wpv_filter_query', 'filter_view_query_fn', 1000 , 3 );
function filter_view_query_fn( $query_args, $view_settings ) {
	// code to limit this function only for a view with specific ID
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 112) ) {

		// view the current query
		echo '<pre>';
		print_r($query_args);
		echo '</pre>';

		// view the parameter's passed through the filters
		echo '<pre>';
		print_r($_GET);
		echo '</pre>';

		// add code to change the query parameters, as needed

	}
	return $query_args;
}

Feel free to adjust the above code as needed and for a more personalized assistance around custom programming, you can consider hiring someone from our list of recommended contractors:
https://toolset.com/contractors/

I hope this helps and please let me know if you have any question around this.

regards,
Waqar

#1145830

Hi Waqar,

I contacted a contractor, but it seems he is not interested...
Your code adjusted a little bit:

add_filter( 'wpv_filter_query', 'filter_view_query_fn', 1000 , 3 );

function filter_view_query_fn( $query_args, $view_settings ) {
    // code to limit this function only for a view with specific ID
    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 112) ) {
/* 		
        // view the current query
        echo '<pre>';
        print_r($query_args);
        echo '</pre>';
 
        // view the parameter's passed through the filters
        echo '<pre>';
        print_r($_GET);
        echo '</pre>';
*/ 		
        // add code to change the query parameters, as needed
        if ($_GET['wpv-wpcf-blend'] == 1) {
			// change query , so it does not search in wpcf-variety , but in (wpcf-variety-member1 OR wpcf-variety-member2 OR ... OR wpcf-variety-member7)
		}
 
    }
    return $query_args;
}

Any suggestions are welcome.

Thank you,
Kostas

#1146611

Hi Kostas,

Based on what you've shared, my understanding is that the role of "Ποικιλία" filter is to provide users to select a value to compare for the other "variety-member" custom fields (1-7).

If that is correct, you can update your code as:
( ref: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters )


function filter_view_query_fn( $query_args, $view_settings )
{
	// code to limit this function only for a view with specific ID
	if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 112) )
	{	
		if( $_GET['wpv-wpcf-blend'] == 1 )
		{
			$query_args['meta_query'] = array(
					'relation' => 'AND',
					array(
						'key'     => 'wpcf-oo-category',
						'value'   => $_GET['wpv-wpcf-oo-category'],
						'type'	  => 'CHAR',
						'compare' => 'LIKE',
					),
					array(
						'relation' => 'OR',
						array(
							'key'     => 'wpcf-variety-member1',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member2',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member3',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member4',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member5',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member6',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
						array(
							'key'     => 'wpcf-variety-member7',
							'value'   => $_GET['wpv-wpcf-variety'],
							'type'	  => 'CHAR',
							'compare' => 'LIKE',
						),
					),
			);
		}
	}

	return $query_args;
}

Note: Please make sure to update your code to match the actual custom field slugs and URL parameters.

regards,
Waqar

#1146643

Thanks a lot Waqar.

Your code works as expected only the first time. All subsequent searches give no results.
You can try it on the site to see what I mean. Whenever $_GET['wpv-wpcf-blend'] == 1, the search gives results only once.

Please advise.

Thanks again,
Kostas

#1147024

Hi Kostas,

I couldn't reproduce this behavior on either my test website or yours.

Can you please share the exact steps to reproduce this, i.e. what values should be selected in each search filter field, to see this?

regards,
Waqar

#1147050

Hi Waqar,

To reproduce, from menu go to "Αναζήτηση ελαιολάδων" ie /oliveoils-finder/
There, for Blend you check "Ναι" (which is Yes) and from "Ποικιλία" you choose "Κορωνέικη (...)" which is 6th from top.
Then you press the button to do the search. This will search all olive oil categories ("Κατηγορία") which are Blends, with one component of a Κορωνέικη variety.
This search is fine and results are shown in /oliveoils-found/?... page, which includes the search form.

From there, all subsequent searches for Blends that include a "Ποικιλία", give no results ("Δεν βρέθηκε τίποτα").
For example, if Blend is "Ναι" and "Ποικιλία" is "Μαστοειδής η κοινή(...)" (11th from top), there are no results, although it exists such a blend. To verify, start a new search by loading /oliveoils-finder/ page. Above settings give 1 result.

Hope this helps,
Kostas

#1147935

Hi Kostas,

Thanks for sharing these details.

I noticed that your view is set to update the search results, with AJAX.
( screenshot: hidden link )

If you'll set the search settings to "Reload the page to update the View results", the custom query filter function will work, as expected.

To make this function work with AJAX results, advance custom code/script changes will be needed to adjust the AJAX calls, which I'm afraid, is beyond the scope of support that we can provide.

regards,
Waqar

#1148448

Hi Waqar,

You are right, it was set to use AJAX, although I do not need AJAX.
Now the search works as expected, thank you very much.

Cheers,
Kostas