Skip Navigation

[Resolved] views and filtering related posts

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 0 replies, has 1 voice.

Last updated by jaredK 1 day, 15 hours ago.

Assisted by: Minesh.

Author
Posts
#2785335

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 (hidden link), 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.

#2785585

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I'm not sure how exactly you added post-relationship filters to your view.

Can you please share problem URL and admin access details. Once I review your current settings I will be able to guide you in the right direction.

*** 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.

#2785793

Minesh
Supporter

Languages: English (English )

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

Ok - so for the Court filter - I've added the following post-relationship query filter to your view.

Filter by post relationship or repeatable fields group owner
Select posts in a Detention Centers Courts relationship that are related to the Post with ID set by the URL parameter court-id.
eg.
hidden link

Now, for the additional parent filter, I've added the following code to "Custom Code" section offered by Toolset:
- hidden link

add_filter( 'wpv_filter_query', 'filter_by_two_parent_func', 99, 3 );
function filter_by_two_parent_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 33916 && ( isset($_GET['partner-id']) and is_numeric($_GET['partner-id']) ) ) {
        if(!isset($query_args['toolset_relationships'])){
            $query_args['toolset_relationships'] = array();
        }
        $query_args['toolset_relationships'][] = array(
                'role' => 'parent',
                'related_to' => $_GET['partner-id'],
                'relationship' => 'detention-center-partner'
        );
    }
    return $query_args;
}
 
 
add_filter('wpv_filter_register_url_parameters_for_posts', 'register_extra_url_param__filterby_two_parent_func', 10, 2);
function register_extra_url_param__filterby_two_parent_func($attributes, $view_settings){
 
    if($view_settings['view_id'] == 33916){
        $attributes[] = array(
        'query_type'=> 'posts',
        'filter_type'=> 'post_relationship',
        'value'=> 'custom_field_value',
        'attribute'=> 'partner-id',
        'expected'=> 'number',
        );
    }
    return $attributes;
}

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

Can you please confirm it works as expected.

#2785912

Thanks for the answer. I would like to make it so it refreshes using AJAX when you choose one of the custom dropdowns, but when I do that, it doesn't refresh. It refreshes when I do city or region but not using the custom dropdown for relationship types.

#2785915

Minesh
Supporter

Languages: English (English )

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

I've adjusted the code you added to following code snippet as given under for Courts dropdown:
- hidden link

function render_court_dropdown() {
    $selected_court_id = isset($_GET['court-id']) ? intval($_GET['court-id']) : '';
    $courts = get_posts(array(
        'post_type' => 'court',
        'posts_per_page' => -1,
    ));

    error_log('Retrieved Courts: ' . print_r($courts, true));

    if (empty($courts)) {
        return '<p>No Courts found.</p>';
    }

    // Add inline style for confining width
    $html = '<select name="court-id" style="max-width: 100%; width: 100%;" class="js-wpv-filter-trigger form-control">';
    $html .= '<option value="" ' . (empty($selected_court_id) ? 'selected' : '') . '>All Courts</option>';
    foreach ($courts as $court) {
        $selected = $selected_court_id == $court->ID ? 'selected' : '';
        $html .= '<option value="' . esc_attr($court->ID) . '" ' . $selected . '>' . esc_html($court->post_title) . '</option>';
    }
    $html .= '</select>';

    return $html;
}
add_shortcode('court_dropdown', 'render_court_dropdown');

I've adjusted the code you added to following code snippet as given under for partners dropdown:
- hidden link

function render_partner_dropdown() {
    $selected_partner_id = isset($_GET['partner-id']) ? intval($_GET['partner-id']) : '';
    $partners = get_posts(array(
        'post_type' => 'txilc-partner',
        'posts_per_page' => -1,
    ));

    error_log('Retrieved Partners: ' . print_r($partners, true));

    if (empty($partners)) {
        return '<p>No Partners found.</p>';
    }

    // Add inline style for confining width
    $html = '<select name="partner-id" style="max-width: 100%; width: 100%;" class="js-wpv-filter-trigger form-control">';
    $html .= '<option value="" ' . (empty($selected_partner_id) ? 'selected' : '') . '>All Partners</option>';
    foreach ($partners as $partner) {
        $selected = $selected_partner_id == $partner->ID ? 'selected' : '';
        $html .= '<option value="' . esc_attr($partner->ID) . '" ' . $selected . '>' . esc_html($partner->post_title) . '</option>';
    }
    $html .= '</select>';

    return $html;
}
add_shortcode('partner_dropdown', 'render_partner_dropdown');

Where I've added the class attribute to both the select dropdown:

class="js-wpv-filter-trigger form-control"

And I've set the custom search setting for your view to filter the result with "AJAX refresh when changing any filter".
=> hidden link
Please check the screenshot: hidden link

Can you please confirm it works as expected while using ajax filtering.

#2785985

Thanks

#2785990

Last question. I made another user and they can't access the area in the edit post that they can attach a related post type. Where is that setting to make sure they have access to that

#2785993

the relationship meta box doesn't show on the edit post