Skip Navigation

[Resolved] Filtering View with two complex Relationships

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 3 voices.

Last updated by Luo Yang 4 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#1635767
Filter_1.png
Filter_2.png

Tell us what you are trying to do?
I want to add two filters on the search view that are related to different relationship.

Is there any documentation that you are following?
https://toolset.com/forums/topic/display-view-with-filter-on-2-parent-posts/
https://toolset.com/forums/topic/search-filter-with-two-post-relation-ships/#post-1216763

The current code on Search and Pagination is

[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group">
  <div class="row no-sidebar-filters mb-2">
    <div class="col-md-7">[wpv-filter-search-box placeholder="Type seaferer's name or vessel's name/IMO" output="bootstrap"]</div>
	<div class="col-md-2">[wpv-filter-submit name="Search" output="bootstrap" class="vsl-button"]</div>
    <div class="col-3">[wpv-sort-orderby type="select" options="post_date,field-wpcf-seaman-firstname,field-wpcf-seaman-lastname" label_for_post_date="Recently Added" label_for_field-wpcf-seaman-firstname="Firstname" label_for_field-wpcf-seaman-lastname="Lastname" orderby_ascending_for="field-wpcf-seaman-firstname,field-wpcf-seaman-lastname" orderby_descending_for="post_date"]</div>
</div>
<div class="row sidebar-filters">
    [wpv-control-post-relationship ancestors="vessel@sea-service-vessel.parent>seaman-sea-service@seaman-seaman-sea-service.child" url_param="wpv-vessel-filter"]
      <div class="form-group">
          <label for="wpv-relationship-filter-vessel">[wpml-string context="wpv-views"]Vessels[/wpml-string]</label>
          [wpv-control-post-ancestor type="checkboxes" label_class="mx-4" ancestor_type="vessel@sea-service-vessel.parent"]
      </div><div class="form-group">
          <label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Seaman Sea Services[/wpml-string]</label>
          [wpv-control-post-ancestor type="checkboxes" label_class="mx-4" ancestor_type="seaman-sea-service@seaman-seaman-sea-service.child"]
      </div>
      [/wpv-control-post-relationship]
  
                [wpv-control-post-relationship ancestors="position-category@seaman-position-category-seaman-position.parent>seaman-position@seaman-seaman-position.parent" url_param="wpv-position-filter"]
      <div class="form-group">
          <h3 class="widget-title sidebar-widget-title mx-3" for="wpv-relationship-filter-position-category">[wpml-string context="wpv-views"]Seafarer Department[/wpml-string]</h3>
          [wpv-control-post-ancestor type="checkboxes" label_class="mx-4" ancestor_type="position-category@seaman-position-category-seaman-position.parent"]
      </div><div class="form-group d-none">
          <h3 class="widget-title sidebar-widget-title mx-3" for="wpv-relationship-filter">[wpml-string context="wpv-views"]Seaman Rank[/wpml-string]</h3>
          [wpv-control-post-ancestor type="checkboxes" label_class="mx-4" ancestor_type="seaman-position@seaman-seaman-position.parent"]
      </div>
      [/wpv-control-post-relationship]
</div>
</div>
[/wpv-filter-controls]
[wpv-filter-end]

on the function.php code I tried to add the below code, but I am missing some thing on how the relationships work.

add_filter( 'wpv_filter_query', 'filter_by_parent_sea_service_vessel_func', 999, 3 );
function filter_by_parent_sea_service_vessel_func( $query_args, $view_settings, $view_id ) {
    if ( $view_id == 456 && isset($_GET['wpv-vessel-filter'])) {
        if(!isset($query_args['toolset_relationships'])){
            $query_args['toolset_relationships'] = array();
        }
        $query_args['toolset_relationships'][] = array(
                'role' => 'child',
                'related_to' => $_GET['wpv-vessel-filter'],
                'relationship' => 'vessel@sea-service-vessel.parent>seaman-sea-service@seaman-seaman-sea-service.child'
        );
    }
    return $query_args;
}


add_filter('wpv_filter_register_url_parameters_for_posts', 'add_extra_url_param_func', 10, 2);
function add_extra_url_param_func($attributes, $view_settings){

	if($view_settings['view_id'] == 456){
		$attributes[] = array(
		'query_type'=> 'posts',
		'filter_type'=> 'post_relationship',
		'value'=> 'custom_field_value',
		'attribute'=> 'wpv-vessel-filter',
		'expected'=> 'number',
		);
	}
	return $attributes;
}
#1637311

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

First of all, I would like to know why you added the view's query filter.

You do not want to display post in relation? Just like State -> City, so when you select any state it should display associated Cities with another dropdown?

Can you please share more details about your issue and share what is your expected output and share the problem URL and access details.

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

#1638279

Minesh
Supporter

Languages: English (English )

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

The thing is that you have two different hierarchy of post-relationship.

1) Seaman Position Categories > Seaman Positions > Seamen
2) Seamen > Seaman Sea Services [*] and Vessels [0 .. 1] << Seaman Sea Services [*]

First thing, you can add only one post-relationship filter to your view's "Search and Pagination" section filter box either for the post-relationship hierarchy 1 or 2.

Currently, views do not offer to add two post-relationship frontend filters. So, the possibility for now is either you can add:
- First filter: I want to be able to filter view records by "Position Category".
or
Second filter: I want to be able to filter view records by "Vessel". This also has to be available as URL param

Regarding:
Third filter: To be able to search by Seaman name or vessel using the search textbox.
--
you can add the Text search to search by Seaman as view's only query the results to the post type the view is set to query. You can not search in the vessel.
More info:
=> https://toolset.com/documentation/user-guides/views/filtering-views-for-a-specific-text-string-search/

#1639013

I will keep the First filter: "Position Category" on the filters View but I will need to have the other filter available like in the link below

https://toolset.com/forums/topic/search-filter-with-two-post-relation-ships/#post-1216763

I want to select a button from another page and the url navigate to the page with the view filtered by the URL parameter only

#1640121

Minesh
Supporter

Languages: English (English )

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

I want to select a button from another page and the url navigate to the page with the view filtered by the URL parameter only
==>
Ok but which page and you want to filter with what post-relationship?

Do you mean that you will keep the following filter:
1) Seaman Position Categories > Seaman Positions > Seamen

And the another post relationship filter should be coming from URL param to filter the post relationship with Vessels? If yes, can you please tell me to which page you will add filter for vassel and pass the vassel ID as URL param?

#1640181
BrowseVessels_listview.png

From the page "browse vessels" after the click of the button circled red in the image. I would be navigated to the above view (Browse Seamen page - Search Seamen view) which would be filtered by "Vessel" in the URL parameter (e.g wpv-vessel-filter).

"And the another post relationship filter should be coming from URL param to filter the post relationship with Vessels? If yes, can you please tell me to which page you will add filter for vassel and pass the vassel ID as URL param?"

So we would like the view to be on the same page of the browse seamen.

#1640825

Hello,

I will take care of this ticket.

I have tried the credentials you provided above:
https://toolset.com/forums/topic/filtering-view-with-two-complex-relationships/#post-1637351

It is not a valid admin account, since it is a custom codes problem, please provide a valid admin account, also provide below details:
- Where I can edit your PHP codes
- I assume we are talking about this case:
1) Goto URL:
hidden link
click button "Find Seamen Worked on Vessel"
2) it redirect me to URL:
hidden link
You are going to display "seamen" posts related with URL parameter "wpv-vessel-filter".
Can you confirm it? thanks

#1641283

Hello, yes I can confirm that this is the case I want to be solve.

I enable the credentials I have provided above and now it is logging as administrator.

Do let me know if there is anything else that you need from me.

#1642605

Thanks for the details, I am checking it in your website, will update here if find anything

#1642687

You are passing URL parameter "wpv-vessel-filter" to the result page, for example:
hidden link

The URL parameter "wpv-vessel-filter" should be a vessel post ID, but one "vessel" post can connect to multiple "Seaman Sea Services" posts, and one "Seaman Sea Services" post can connect to one "seamen" post, as a result:
one "vessel“ post post is related with multiple "seamen" posts.

According to our document:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/#full-documentation-of-the-query-argument
parameter "related_to" accept only one post, so your custom PHP codes won't work.

Here is a workaround, you can use "vessel" post ID to get related "Seaman Sea Services" posts IDs:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Then use above "Seaman Sea Services" posts IDs to get related "seamen" posts IDs:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Then apply above "seamen" posts IDs into view's query:
https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters
post__in (array) – use post ids. Specify posts to retrieve

For your reference.