Home › Toolset Professional Support › [Resolved] How to modify view’s query on fly
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 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)
Tagged: Custom search, Views plugin
Related documentation:
This topic contains 26 replies, has 2 voices.
Last updated by Kostas 5 years, 10 months ago.
Assisted by: Minesh.
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?
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.
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?
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!
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.
Check against wpcf-variety
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.
Could you please confirm that solution I shared works for you 🙂
It seems it works, for all filters, *except* wpcf-variety is not searched in wpcf-variety-memberX.
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?
>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.
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?
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).
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 🙂
Could you please mark resolve this ticket 🙂
Feel free to open a new ticket with your each new question you may have.