Tell us what you are trying to do?
I have a view that displays two cpts Containers and Shipments, and it has a custom parent relationship filter that Minesh helped me with here (https://toolset.com/forums/topic/filter-view-by-parent-post/). I am now attempting to add a date search that combines two custom date fields, Container Confirmation Date and Shipment Confirmation Date.
I'm running into two issues now. First if I input a date (I've tried with text and datepicker inputs) but do not select a warehouse (parent filter mentioned above) I get a blank page. Second, if I select a warehouse, I get results but it doesn't apply the date filter.
I thought this may have something to do with only being able to use the AND operator within the view, so I found the fix for that here (https://toolset.com/errata/and-always-used-with-multiple-views-query-filters-even-if-or-is-specified/), unfortunately that causes a critical error on the site. I reformatted it a bit and removed the array in favor of a single view id and it has no affect. Thought it would be good to mention.
I'll provide access to the site and further info in my follow-up.

Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Could you please share admin access details and problem URL and exact steps I will have to follow to see both the issues.
*** 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.

Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
I can see that you have added the custom field filter as date filter for the custom field "wpv-conf-date" - do you mean that you want to match the fields Container Confirmation Date and Shipment Confirmation Date equal to selected conf-date?
Yes that's what I am looking to do Minesh. Let me know if you need further info!
Thanks,
Matt

Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
Can you please check now.
I've adjusted the code added to "Custom Code" section as given udner:
add_filter('wpv_filter_query', 'func_filter_by_multiple_relationships_same_parent',99,3 );
function func_filter_by_multiple_relationships_same_parent( $query_args, $view_settings, $view_id ) {
if ( $view_id == 758 && isset($_GET['wpv-relationship-filter']) and $_GET['wpv-relationship-filter']!=0) {
$parent_id = $_GET['wpv-relationship-filter'];
$related_containers = toolset_get_related_posts(
$parent_id,
'warehouse-container',
'parent',
999,
0,
array(),
'post_id',
'child'
);
$related_shipments = toolset_get_related_posts(
$parent_id,
'warehouse-shipments',
'parent',
999,
0,
array(),
'post_id',
'child');
$query_args['post__in'] = array_merge($related_shipments,$related_containers);
}
return $query_args;
}
I can see the results it shows is less than the selected date.
Hi Minesh,
Unfortunately it doesn't appear to be working. I tested by selecting the date April 15th 2023 and it is still returning posts with dates prior to that as seen in the screenshot. I tested both with and without assigning a warehouse filter, same results.
Thanks,
Matt

Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
I see why its not working could be because I see there is another filter added that applies OR condition for all the custom field query.
add_filter( 'wpv_filter_query', 'wpv_filter_query_func', 1000 , 3 );
function wpv_filter_query_func( $query_args, $view_settings ) {
// process if specific view
if ( ( isset($view_settings['view_id']) && $view_settings['view_id'] == 758) ) {
if( !empty($query_args['meta_query']) ) {
$query_args['meta_query']['relation'] = 'OR';
}
}
return $query_args;
}
What clause you want to apply between your query filters?
Select items with field:
Container Status is a number equal to 2
AND
Shipment Status is a string equal to 2
AND
Shipment Confirmed Date is a number lower than or equal URL_PARAM(wpv-conf-date)
AND
Container Confirmed Date is a number lower than or equal URL_PARAM(wpv-conf-date)
is above is correct?
I think for this to work correctly it would need to have mixed AND/OR conditions something like this:
Select items with field:
[Container Status is a number equal to 2
AND
Container Confirmed Date is a number lower than or equal URL_PARAM(wpv-conf-date)]
OR
[Shipment Status is a string equal to 2
AND
Shipment Confirmed Date is a number lower than or equal URL_PARAM(wpv-conf-date)]
I tested by turning off the 'query-with-or' code snippet, and I saw no change in search behavior. But I'm not sure why using AND returns any results at all. A single post in this view (container/shipment) can't both be Container Status = 2 and Shipment Status = 2 so how is that returning anything at all?
I really appreciate your help on this Minesh, it's been driving me nuts for a bit now.

Minesh
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
Asia/Kolkata (GMT+05:30)
Can you please check now:
=> lien caché
I've adjusted the code added to the "Custom Code" section as given under:
add_filter('wpv_filter_query', 'func_filter_by_multiple_relationships_same_parent',99,3 );
function func_filter_by_multiple_relationships_same_parent( $query_args, $view_settings, $view_id ) {
if ( $view_id == 758) {
$meta_args = $query_args['meta_query'];
$remove_meta_args = array();
$remove_meta_args = array('wpv-conf-date');
foreach($meta_args as $k=>$v):
if( in_array($v['key'],$remove_meta_args) ) {
unset($query_args['meta_query'][$k]);
}
endforeach;
if(isset($_GET['wpv-relationship-filter']) and $_GET['wpv-relationship-filter']!=0) {
$parent_id = $_GET['wpv-relationship-filter'];
$related_containers = toolset_get_related_posts(
$parent_id,
'warehouse-container',
'parent',
999,
0,
array(),
'post_id',
'child'
);
$related_shipments = toolset_get_related_posts(
$parent_id,
'warehouse-shipments',
'parent',
999,
0,
array(),
'post_id',
'child');
$query_args['post__in'] = array_merge($related_shipments,$related_containers);
}
if(isset($_GET['wpv-conf-date']) and $_GET['wpv-conf-date']!='') {
$start = $_GET['wpv-conf-date'];
$end = $_GET['wpv-conf-date']+(60*60*24);
$new_meta_query = array('relation'=>'OR',
array('relation'=>'AND',
array('key'=>'wpcf-container-status',
'value'=>2,
'type'=>'NUMERIC',
'compare'=>'='),
array('key'=>'wpcf-container-confirmed-date',
'value'=>"$start,$end",
'type'=>'NUMERIC',
'compare'=>'BETWEEN'),
),
array('relation'=>'AND',
array('key'=>'wpcf-shipment-status',
'value'=>2,
'type'=>'NUMERIC',
'compare'=>'='),
array('key'=>'wpcf-shipment-confirmed-date',
'value'=>"$start,$end",
'type'=>'NUMERIC',
'compare'=>'BETWEEN'),
),
);
$query_args['meta_query'] = $new_meta_query;
}
}
return $query_args;
}
Can you please confirm it works as expected.
Thanks Minesh! Fantastic support sir!