Skip Navigation

[Resolved] Filter view by parent post

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 7 replies, has 2 voices.

Last updated by MattI4840 1 year, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2597907

Tell us what you are trying to do?

I have three custom post types, Warehouses, Containers and Shipments, with the following relationships:
Warehouses-Containers (one to many)
Warehouses-Shipments (one to many)

I have created a view that displays all Containers and Shipments, now I need to add a filter to limit the results to the children of the selected Warehouse (drop down filter). So basically display all Container and Shipment children for the warehouse selected.

The built in options for filtering by relationship wont work because it relies on two seperate relationships. Is there a recommended way to approach this type of filtering?

Thanks!

#2598591

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I would like to know first how you setup your view. Have you attach your view to display multiple post types?

Additionally, Could you please send me debug information that will help us to investigate your issue.
=> https://toolset.com/faq/provide-debug-information-faster-support/

#2598687
Screenshot 2023-04-24 091002.png

Hi Minesh,

I'm attaching an image of the view settings so far, I do have both cpts selected for the view. It is a local dev instance, but if you need to take a look I move it to my live server just let me know.

Thanks,
Matt

#2599167

Minesh
Supporter

Languages: English (English )

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

Yes please. Can you please share staging site and let me see if I can offer you any possible workaround if exists.

Please share problem URL where I can see the view and what is your expected result and on what post you want to click and display related posts.

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

#2600109

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL where you added your view?

#2600201

After you login the view is on the homepage, hidden link is the URL.

#2600709

Minesh
Supporter

Languages: English (English )

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

I've added the "Warehouses" filter to your view:
- hidden link

I've added the following filter to "Custom Code" section offered by Toolset:
=> hidden link

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'])) {
      
      $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 now you will be able to filter with Warehouses:
- hidden link

More info:
- https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

#2601077