Home › Toolset Professional Support › [Resolved] Filters with Address fields and Repeatable fields in the same filter
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 9 replies, has 2 voices.
Last updated by Minesh 2 years, 12 months ago.
Assisted by: Minesh.
I would like to query with four variables. Three of them are inside of the same repeating field. The forth is the city of a connected address field. The content type is parish, it has a repeating field of mass times. Mass times has three variables: Day of the week, Language, Time.
I have setup a view, Post type Parish, with filters of Language, times, and city.
When I add the city in, it doesn't work.
Here is the custom query
function custom_search_criteria( $query_args ,$view_settings ) {
if (isset($view_settings['view_id']) && $view_settings['view_id'] == 59863 ) {
$index = 0;
if(isset($query_args['meta_query'])){
$index = count($query_args['meta_query'])+1;
}
$mass_time = $_GET['mass-time'][0] ;
$mass_end_time=$mass_time+400;
$mass_day = $_GET['mass-day'];
$mass_city = $_GET['city'];
$mass_language = $_GET['mass-language'];
$query_args['meta_query'][$index] = array(
'relation' => 'AND',
array(
'key' => 'wpcf-masstimes-start_time',
'value' => ($mass_time .",".$mass_end_time),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'wpcf-masstimes-day_of_the_week',
'value' => $mass_day,
'compare' => 'IN',
),
array(
'key' => 'wpcf-masstimes-language',
'value' => $mass_language,
'compare' => 'IN',
),
array(
'key' => 'wpcf-parish_address_locality',
'value' => $mass_city,
'compare' => 'IN',
)
);
}
return $query_args;
}
This is the MYSQL it generates:
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1
ON ( wp_posts.ID = mt1.post_id )
INNER JOIN wp_postmeta AS mt2
ON ( wp_posts.ID = mt2.post_id )
INNER JOIN wp_postmeta AS mt3
ON ( wp_posts.ID = mt3.post_id )
WHERE 1=1
AND ( ( ( wp_postmeta.meta_key = 'wpcf-masstimes-start_time'
AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN '1000'
AND '1400' )
AND ( mt1.meta_key = 'wpcf-masstimes-day_of_the_week'
AND mt1.meta_value IN ('Sunday','','Sunday') )
AND ( mt2.meta_key = 'wpcf-masstimes-language'
AND mt2.meta_value IN ('French') )
AND ( mt3.meta_key = 'wpcf-parish_address_locality'
AND mt3.meta_value IN ('Regina') ) ) )
AND wp_posts.post_type IN ('parish', '45mass_times')
AND ((wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title DESC
BUT the line for wpcf-parish_address_locality is not correct. Because the mass times is a repeating field the city and the repeating fields won't be in the same line.
I *think what I need is to have a filtered query with the IDs of the parishes First and check if it is that list, but I don't know how to do that.
Does that even make sense?
Content type: Parish
Fields :
Address Field (with a city)
Repeating items field with Day of the week, Language, Time
other fields
A working Drupal version of this is here: hidden link
Hello. Thank you for contacting the Toolset support.
I will have to look at and review your structure first and then I will be able to guide you in the right direction.
Can you please share problem URL for what page I can see the issue as well as where you added the code you shared.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
Ok, I'm not sure why you added the custom search filters to "Output Editor ", that is not the right place to add the filters.
First of all, when you have custom search you should assign the it only one post type.
Please check the following doc to understand how you build the custom search:
- https://toolset.com/documentation/legacy-features/views-plugin/front-page-filters/
Also, when you added the repeating field group, the repeating field group item will be considered as child post hence its treated as one-to-many post relationship internally.
So, city field is belong to parent post type and all repeating field group fields belongs to child post type (Repeating field group slug treated as child post type).
I suggest you should first adjust the view correctly and then:
- set your view to query only repeating field group and add fields belongs to parent (city) and child (mass-day, mass-language,mass-time) as filters to search and pagination section.
- then you will have to adjust the code as you will have to adjust the code and build a query to query parent post type "parish" to search for city value and run the query and get the result ids and then you should run another custom query to find the results based on fields (mass-day, mass-language,mass-time) and get the ids and then combine both ids.
I'm still not sure what is your goal, do you want to display repeating field group items as result or parent post information because one parent can have multiple RFG items as result and RFG items can have same parent.
Wow. The query is actually much faster using your methods. I am not sure why.
I decided to start over, that way I can learn better methods.
From your post, I was doing many things incorrectly, which was the result of searching through endless posts and not knowing where to begin.
hidden link
I have setup a new view, and querying only the repeating group. I have added the filters for the repeating items and they seem to be working correctly.
I don't seem to be able to add the city of each. When I add the field, it comes up as blank. When I add an ancestor it shows as the title of the parent post, which I don't need or want.
You weren't sure what my goal is. This is my goal: hidden link I can filter by city, day, language and time.
The final key is to be able to filter by the parent city.
[wpv-filter-start hide="false"] [wpv-filter-controls] [wpv-control-post-relationship ancestors="parish@45mass_times.parent" url_param="wpv-relationship-filter"] <div class="form-group"> <label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Parishs[/wpml-string]</label> [wpv-control-post-ancestor type="select" ancestor_type="parish@45mass_times.parent"] </div> [/wpv-control-post-relationship] <div class="form-group"> <label for="wpv-wpcf-masstimes-day_of_the_week">[wpml-string context="wpv-views"]Mass Times Day of the Week[/wpml-string]</label> [wpv-control-postmeta type="select" field="wpcf-masstimes-day_of_the_week" default_label="ANY" url_param="wpv-wpcf-masstimes-day_of_the_week"] </div> <div class="form-group"> <label for="wpv-wpcf-masstimes-start_time">[wpml-string context="wpv-views"]Weekday Mass Times[/wpml-string]</label> [wpv-control-postmeta display_values="ANY,7:00 AM,8:00 AM,9:00 AM,10:00 AM,11:00 AM,12:00 PM,1:00 PM,2:00 PM,3:00 PM,4:00 PM,5:00 PM,6:00 PM,7:00 PM,8:00 PM" values="0,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000" field="wpcf-masstimes-start_time" type="select" source="custom" url_param="wpv-wpcf-masstimes-start_time"] </div> <div class="form-group"> <label for="wpv-wpcf-masstimes-language">[wpml-string context="wpv-views"]Mass Times Language[/wpml-string]</label> [wpv-control-postmeta field="wpcf-masstimes-language" url_param="wpv-wpcf-masstimes-language"] [wpv-filter-submit name="SEARCH" output="bootstrap" class="btn-secondary fusion-button button-flat fusion-button-default-size button-default button-13 fusion-button-default-span fusion-button-default-type inlinebutton"] [wpv-filter-reset output="bootstrap" class="fusion-button button-flat fusion-button-default-size button-default button-13 fusion-button-default-span fusion-button-default-type inlinebutton"] </div>[/wpv-filter-controls] [wpv-filter-end]
Assistance?
I do not see you added the city filter but again city field belongs to parent, with the custom search view you can only add the fields that belongs to the post type you set your view to query. As you set your view to query the repeating field group "Mass Times" you can add the fields belongs to the repeating field group "Mass Times".
I notice that you setup the relationship filter "Parishs", does it works as expected? Have you changed the plan to do not add the city filter?
I was attempting to add the city, but it would come up as the parish name.
I have the three fields, but I need to add the city filter.
I don't know how to do this:
- then you will have to adjust the code as you will have to adjust the code and build a query to query parent post type "parish" to search for city value and run the query and get the result ids and then you should run another custom query to find the results based on fields (mass-day, mass-language, mass-time) and get the ids and then combine both ids.
Can you show me how to accomplish that?
So far I have
mass_city = isset($_GET['city']) ? $_GET['city'] : ''; $parishes = new WP_Query( array( 'post_type' => array('parish'), 'posts_per_page' => -1, 'author__in'=>array($current_user->ID), 'fields'=>"ids", 'meta_query' => array( 'relation'=>"AND", array( 'key' => 'wpcf-parish_address_locality', 'value' => $mass_city, 'compare' => 'IN'), ) )); $found_parishes = $parishes->posts;
That gives me the post_id(s) of the City queried. What I don't know is how to take that to filter the Post_IDs in the resulting repeating field query that is running on the page.
I've set your view to query "parish" post type as well as repeating field group "45mass_times".
=> hidden link
Then, I've adjusted the filter code you added to your child theme as given under:
function func_cityparishes_posts( $query_args ,$view_settings, $view_id ) { global $post; global $current_user; global $WP_Views; if ($view_id == 59874 and isset($_GET['wpv-wpcf-parish_address_locality'])) { $query_args['post_type']= array('45mass_times'); $index = array_search('wpcf-parish_address_locality', array_column($query_args['meta_query'], 'key')); unset($query_args['meta_query'][$index]); $mass_city = isset($_GET['wpv-wpcf-parish_address_locality']) ? $_GET['wpv-wpcf-parish_address_locality'] : ''; $parishes = new WP_Query( array( 'post_type' => array('parish'), 'posts_per_page' => -1, /* 'author__in'=>array($current_user->ID), */ 'fields'=>"ids", 'meta_query' => array( 'relation'=>"AND", array( 'key' => 'wpcf-parish_address_locality', 'value' => $mass_city, 'compare' => 'IN'), ) )); $found_parishes = $parishes->posts; $related_ids = array(); foreach($found_parishes as $k=>$v): $related_ids[] = toolset_get_related_posts( $v, '45mass_times', 'parent', 999, 0, array(), 'post_id', 'child' ); endforeach; $final = array_merge(...$related_ids); $query_args['post__in'] = $final; } return $query_args; } add_filter( 'wpv_filter_query', 'func_cityparishes_posts', 10, 3);
Can you please confirm it works as expected?
It does work as expected, thank you. There is one issue I would like to work out though. Is it possible to group them into just one listing?
hidden link
The original search (Drupal) has just one showing: hidden link
I am using a view on the right column, which I think I can customize to just display the mass times searched.
I tried adding query_arg for groupby but that isn't working.
Could you please check now. I've adjusted the code added to the funcitons.php file as given under:
function func_cityparishes_posts( $query_args ,$view_settings, $view_id ) { global $post; global $current_user; global $WP_Views; if ($view_id == 59874) { if(isset($_GET['wpv-wpcf-parish_address_locality']) and $_GET['wpv-wpcf-parish_address_locality'] != '') { $query_args['post_type']= array('parish'); $index = array_search('wpcf-parish_address_locality', array_column($query_args['meta_query'], 'key')); unset($query_args['meta_query'][$index]); $mass_city = isset($_GET['wpv-wpcf-parish_address_locality']) ? $_GET['wpv-wpcf-parish_address_locality'] : ''; $query_args['meta_query'] = array(); $parishes = new WP_Query( array( 'post_type' => array('parish'), 'posts_per_page' => -1, /* 'author__in'=>array($current_user->ID), */ 'fields'=>"ids", 'meta_query' => array( 'relation'=>"AND", array( 'key' => 'wpcf-parish_address_locality', 'value' => $mass_city, 'compare' => 'IN'), ) )); $found_parishes = $parishes->posts; $query_args['post__in'] = $found_parishes; } else { $mass_time = $_GET['wpv-wpcf-masstimes-start_time'] ; if($mass_time == 0) { $mass_time_args = array(); } $mass_day = $_GET['wpv-wpcf-masstimes-day_of_the_week']; $mass_language = $_GET['wpv-wpcf-masstimes-language']; $query_args['meta_query'] = array(); $find_child_posts = new WP_Query( array( 'post_type' => array('45mass_times'), 'posts_per_page' => -1, 'fields'=>"ids", 'meta_query' => array( 'relation' => 'AND', $mass_time_args, array( 'key' => 'wpcf-masstimes-day_of_the_week', 'value' => $mass_day, ), array( 'key' => 'wpcf-masstimes-language', 'value' => $mass_language, )))); $found_parents = array(); if(!empty($find_child_posts->posts)) { foreach($find_child_posts->posts as $k=>$v): $x = toolset_get_related_post($v,'45mass_times'); $found_parents[$x] = $x; endforeach; $query_args['post__in'] = array_keys($found_parents); }else{ $query_args['post__in'] = array(00); } } } return $query_args; } add_filter( 'wpv_filter_query', 'func_cityparishes_posts', 10, 3);
I've also done some adjustment to both the view's output:
- hidden link
- hidden link
Can you please confirm it works as expected now.