Skip Navigation

[Resolved] How to modify view’s query on fly

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

Problem:
How to modify view's query on fly with "AND" and "OR" clause for custom fields

Solution:

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/not-completed-query/page/2/#post-1168622

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

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 26 replies, has 2 voices.

Last updated by Kostas 6 years ago.

Assisted by: Minesh.

Author
Posts
#1168430

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I would like to know that, I see you added following fields that start from "wpcf-variety-member1" to "wpcf-variety-member7". I see there is no filter added for these fields, from where we need to catch the value?

#1168435

If you to hidden link and choose Blend second choice (not the default which is No), then "wpcf-variety-member1" and "wpcf-variety-member2" should appear with the choice to add more fields, up to "wpcf-variety-member7". These are the component (member) varieties of a blend. They should be at least 2 and up to 7.

#1168461

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - but thats a Toolset Form and when I see the view, I do not see any related filter is added for fields "wpcf-variety-member1" and "wpcf-variety-member2" etc....

When I select "Yes" (Ναι) on the following link: hidden link

I do not see any filters available for "wpcf-variety-member1" and "wpcf-variety-member2" ... - Do you need filters for these fields? Have you added it? If yes, what exactly the steps to see it? Or you are just querying those filters in the backend using view's filter only?

#1168488

The way the search is designed now, is this:

If wpcf-blend is "No" (Όχι), then we search for wpcf-variety and the query is shown in the Oliveoils finder view.

If wpcf-blend is "Yes" (Ναι), then we search for wpcf-variety-member1 OR wpcf-variety-member2 OR ... OR wpcf-variety-member7, and the query is shown in functions.php (wpv_filter_query filter).

I do not know if this design is good practice, I am open to suggestions!

#1168519

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If wpcf-blend is "No" (Όχι), then we search for wpcf-variety and the query is shown in the Oliveoils finder view.
==> OK

If wpcf-blend is "Yes" (Ναι), then we search for wpcf-variety-member1 OR wpcf-variety-member2 OR ... OR wpcf-variety-member7, and the query is shown in functions.php (wpv_filter_query filter).
==> OK, but what value you will check against the fields wpcf-variety-member1 OR wpcf-variety-member2 OR ... OR wpcf-variety-member7? for what value needs to compare as there is no field is added to filter.

#1168540

Check against wpcf-variety

#1168622

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now, I've adjust the code as given under:

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) ){   
	
        if( $_GET['wpv-wpcf-blend'] == 1 ){
					
			
			//$configured_args = $query_args['meta_query'];
			
			if(isset($_GET['wpv-wpcf-variety']) and $_GET['wpv-wpcf-variety']!=''){
				
			
							$query_args['meta_query'][] = 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;
}

I can see it works and there is no timeout error. Could you please confirm.

#1168655

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please confirm that solution I shared works for you 🙂

#1168660

It seems it works, for all filters, *except* wpcf-variety is not searched in wpcf-variety-memberX.

#1168664

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean that when no option from wpcf-variety select box is selected it does not work? If yes:
Well - it should be expected as there is no value to assign the fields wpcf-variety-memberX as there is no selection made from wpcf-variety.

What should be the value assigned to wpcf-variety-memberX when no value selected from wpcf-variety?

#1168669

>Do you mean that when no option from wpcf-variety select box is selected it does not work? If yes:
>Well - it should be expected as there is no value to assign the fields wpcf-variety-memberX as there is no selection made >from wpcf-variety.

When no option from wpcf-variety select box is selected it does work. It searches for all varieties. That is OK.

If there is a selection from wpcf-variety select box, it does not narrow it to that member variety. That is not OK.

#1168671

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well- Could you please share a test case example of what option should be selected with wpcf-variety select box and what is your expected results?

#1168687

OK, load hidden link
then move first slider (quantity) to 200:500 and also check Blend second option (Ναι).
Push the search button (ΑΝΑΖΗΤΗΣΗ), you get 5 results.

First result (615403017) has 2 variety members. If you search for each of them (set in wpcf-variety select box), you get nothing. It should give at least 1 result (615403017).

#1168712

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you were not getting results because First result (615403017) has 2 variety members but when I check the post here:
=>hidden link

There was no value assigned for the field "wpcf-variety" and you are selecting the value from wpcf-variety select box, so, for example, we selected the option "Κορωνέικη (Κρητικιά, Λαδολιά, Νανάκι, Ψιλολιά, Βάτσικη, Σταφυλολιά)" but when I check above post for which I shared the link the value was not set for the field "wpcf-variety".

I set the same option "Κορωνέικη (Κρητικιά, Λαδολιά, Νανάκι, Ψιλολιά, Βάτσικη, Σταφυλολιά)" in backend post and I see now its displaying the expected results. So either you should remove the "wpcf-variety" filter from view when you filter the view or your data should be set correctly.

Finally, I hope we can close this long thread 🙂

#1169389

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please mark resolve this ticket 🙂

Feel free to open a new ticket with your each new question you may have.