Project Summary
Objective
Create a Toolset View for Parent Centers that allows filtering based on two related post types:
ChildAs: Parent Centers ↔ ChildAs (via Parent-center-ChildA relationship).
ChildBs: Parent Centers ↔ ChildBs (via Parent-center-ChildB relationship).
Expected Behavior
The View should display all Parent Centers by default.
Selecting a ChildA and/or ChildB from dropdowns should filter the results accordingly.
A "Reset Filters" button should clear all filters and reset the dropdowns to their default state (e.g., "All ChildAs" and "All ChildBs").
Current Setup
Relationships
Parent-center-ChildA:
Links Parent Centers to ChildAs.
Many-to-Many relationship.
Parent-center-ChildB:
Links Parent Centers to ChildBs.
Many-to-Many relationship.
View Configuration
The View queries Parent Centers.
Dropdown filters for ChildAs and ChildBs are added via shortcodes.
A custom PHP filter modifies the query to apply the selected filters.
Code Implemented
Dropdown Shortcodes
ChildA Dropdown:
php
Copy code
function render_ChildA_dropdown() {
$selected_ChildA_id = isset($_GET['ChildA-id']) && !empty($_GET['ChildA-id']) ? intval($_GET['ChildA-id']) : '';
$ChildAs = get_posts(array(
'post_type' => 'ChildA',
'posts_per_page' => -1,
));
$html = '<select name="ChildA-id">';
$html .= '<option value="" ' . (empty($selected_ChildA_id) ? 'selected' : '') . '>All ChildAs</option>';
foreach ($ChildAs as $ChildA) {
$selected = $selected_ChildA_id == $ChildA->ID ? 'selected' : '';
$html .= '<option value="' . esc_attr($ChildA->ID) . '" ' . $selected . '>' . esc_html($ChildA->post_title) . '</option>';
}
$html .= '</select>';
return $html;
}
add_shortcode('ChildA_dropdown', 'render_ChildA_dropdown');
ChildB Dropdown:
php
Copy code
function render_ChildB_dropdown() {
$selected_ChildB_id = isset($_GET['ChildB-id']) && !empty($_GET['ChildB-id']) ? intval($_GET['ChildB-id']) : '';
$ChildBs = get_posts(array(
'post_type' => 'txilc-ChildB',
'posts_per_page' => -1,
));
$html = '<select name="ChildB-id">';
$html .= '<option value="" ' . (empty($selected_ChildB_id) ? 'selected' : '') . '>All ChildBs</option>';
foreach ($ChildBs as $ChildB) {
$selected = $selected_ChildB_id == $ChildB->ID ? 'selected' : '';
$html .= '<option value="' . esc_attr($ChildB->ID) . '" ' . $selected . '>' . esc_html($ChildB->post_title) . '</option>';
}
$html .= '</select>';
return $html;
}
add_shortcode('ChildB_dropdown', 'render_ChildB_dropdown');
PHP Query Filter
php
Copy code
add_filter('wpv_filter_query', 'custom_many_to_many_relationship_query', 10, 3);
function custom_many_to_many_relationship_query($query_args, $view_settings, $view_id) {
if ($view_id == 33009) { // Replace with your View ID
$meta_query = array('relation' => 'AND');
// Filter by ChildA
if (!empty($_GET['ChildA-id']) && is_numeric($_GET['ChildA-id'])) {
$related_Parent_centers = toolset_get_related_posts(
sanitize_text_field($_GET['ChildA-id']),
'Parent-center-ChildA',
'parent',
-1
);
if (!empty($related_Parent_centers)) {
$meta_query[] = array(
'key' => 'ID',
'value' => $related_Parent_centers,
'compare' => 'IN',
);
}
}
// Filter by ChildB
if (!empty($_GET['ChildB-id']) && is_numeric($_GET['ChildB-id'])) {
$related_Parent_centers = toolset_get_related_posts(
sanitize_text_field($_GET['ChildB-id']),
'Parent-center-ChildB',
'parent',
-1
);
if (!empty($related_Parent_centers)) {
$meta_query[] = array(
'key' => 'ID',
'value' => $related_Parent_centers,
'compare' => 'IN',
);
}
}
// Apply meta query
if (!empty($meta_query)) {
$query_args['meta_query'] = $meta_query;
}
}
return $query_args;
}
Steps Taken
Dropdowns Rendered Correctly:
Dropdowns display all available ChildAs and ChildBs.
Selected options persist after submission.
Filtering Logic Implemented:
toolset_get_related_posts is used to retrieve related Parent Centers based on the selected ChildA or ChildB.
Reset Filters:
A "Reset Filters" button was added using [wpv-filter-reset-url] to clear parameters and reset the dropdowns.
Issues Encountered
Filters Do Not Apply:
Even with parameters (ChildA-id and ChildB-id) in the URL, the results are not filtered correctly.
Reset Filters Behavior:
When visiting the bare URL (lien caché), previous selections persist in the dropdowns and results do not reset to the default state.
Debugging toolset_get_related_posts:
Debug logs show no related Parent Centers retrieved for valid IDs in some cases.
Debugging Outputs
Here’s an example of what was logged during debugging:
Incoming Parameters:
csharp
Copy code
Array
(
[ChildA-id] => 33594
[ChildB-id] => 33120
)
Related Parent Centers (ChildA):
javascript
Copy code
Array ( )
Related Parent Centers (ChildB):
javascript
Copy code
Array ( )
Assistance Needed
Validate Relationship Queries:
Are the Parent-center-ChildA and Parent-center-ChildB relationships correctly referenced with toolset_get_related_posts?
Reset Filters Behavior:
How to ensure that dropdowns reset correctly and results display all Parent Centers when no parameters are present?
Filtering Logic:
Confirm if the approach using meta_query and toolset_get_related_posts is valid for this use case.