Skip Navigation

[Resolved] View query filter gets added to another field

This support ticket is created 2 years, 10 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
- 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 18 replies, has 2 voices.

Last updated by Arno 2 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2352625
toolset-view-query-filters-filter-added.jpg
toolset-view-query-filters-original.jpg

Hello,

I have a view with multiple custom search fields and it works fine. The view query contains the frontend filters.

I wanted to add a filter that is not in the frontend, on a number field of a post relation. I managed to add it as you can see in the screenshot. But now the view does not return any results and all filters (dropdowns) are empty.

The number fields are filled in all post relationships. Although even if omitted this should work I guess.

What could cause this and how do I debug this unexpected result?

Edit: sorry, a better title for this thread would be "Query filter on number field of post relationship breaks the view".

#2353675

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL and admin access details and let me check whats going wrong with your setup.

Can you please share details what is your expected results with test example.

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

#2354183

Minesh
Supporter

Languages: English (English )

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

The reason why you do not see any result as the field for which you add the query filter "Travel ticket discount on activity discount percentage" belongs to the intermediary post type.

When you build the custom search, you can add frontend filter or query filter for fields or taxonomies that is directly belongs to the post type you set your view to query. In your case you set your view to query the post type "Travel Tickets" so you can add frontend filter or query filter for fields/taxonomies that belongs to the post type "Travel Tickets".

I see you setup four travel tickets:
- hidden link

I see three of them have the child post setup with intermediary post type having custom field. The travel ticket post "Montreux Riviera Card" does not have any child post setup having said that there is no intermediary post type setup having custom field.

Do you mean that when a post does not have any child post, you want to exclude that post from result or is there any other requirement like for instance you have setup a post A with three child activity and out of those three only two child have the "Travel ticket discount on activity discount percentage" greater than equal to zero, (for instance you set the field value to 25 for child post 1 and 15 for child post 2). And you want to include post A in result as it has at least one post (actually we have 2) where "Travel ticket discount on activity discount percentage" field value is set as (25 and 15).

Can you please tell me your requirement and accordingly I can see if we can do anything further.

#2354197

Hi Minesh,

Thank you for looking into this. Ok, so query filters should be on fields of the queried post type, not on related post types.

The requirement is:
IF
(user is using custom search field Activities)
THEN
((only return Travel tickets related to the selected Activities) AND ("Travel ticket discount on activity discount percentage" on the Travel ticket-Activity relation > 0))

If a Travel ticket post has no Activity child, it should only be in the output if the user is not using the custom search field Activities.

If a Travel ticket post has multiple Activity children, it should be in the output if the user is not using the custom search field Activities, or if "Travel ticket discount on activity discount percentage" is > 0 for the Activities the user is searching for.

Worst case, our content editing procedure should be to never configure Travel ticket-Activity relations if the percentage is 0. That way we don't need to filter. But we would lose functionality on the template for a single Travel ticket.

I hope I explained well 🙂

#2355005

Minesh
Supporter

Languages: English (English )

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

There is no way to filter the related post frontend filter dropdown.

I do not see any record where you setup the "Travel ticket discount on activity discount percentage" field to 0. Could you please setup couple of records where you setup "Travel ticket discount on activity discount percentage" to 0 and send me those URLs where you setup that. I've one workaround in mind that what we can do it found all records having "Travel ticket discount on activity discount percentage" value to 0 and get those intermediary post IDs and based on those Ids I will try to use javascript to remove those records from the relationship filter dropdown.

I would like to implement that if possible and working and share you with workaround.

#2355121

I added an Activity relation with "Travel ticket discount on activity discount percentage" set to 0, on two Travel tickets. Here are the links:

hidden link
hidden link

The Javascript approach sounds rather complex but also interesting to take a look at 🙂

#2355185

Minesh
Supporter

Languages: English (English )

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

I've created the following view that will return the IDs of activity posts that has "Travel ticket discount on activity discount percentage" value set to 0.
=> hidden link

At top of your content template I've added the HTML block and within that block I've added the hidden field as given under:
=> hidden link

<input type="hidden" name="activity_remove_ids" id="activity_remove_ids" value='[wpv-view name="filter-get-all-activities-ids"]' />

I've added the following code to your content template's JS editor:

jQuery(document).ready(function($) {
     remove_values = $.unique($.trim($('#activity_remove_ids').val()).split(","));

jQuery.each( remove_values, function( i, val ) {
  $("select[name='wpv-relationship-filter[]']  option[value="+val+"]").remove();
});

I see the "City Walk Zurich" activity is removed from the relationship dropdownbox:
=> hidden link

Can you please confirm it works as expected.

#2355337

Many thanks for your efforts Minesh. I notice two things:
1) on page load, 2 attractions are in the dropdown. Indeed it makes sense that the City Walk is not listed because it only has a percentage of 0 for all Travel tickets. But if I click on one of the attractions in the dropdown, it loads the City Walk anyway.
2) If I now select the City Walk and click Submit, it outputs the two Travel tickets that have a relationship with this Attraction but since the value is 0 I'd expect no Travel tickets to be returned.

#2355341

Minesh
Supporter

Languages: English (English )

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

Could you please check now. I've added the following JS code to your view's "Custom JS" box, screenshot:
=> hidden link

jQuery( document ).on( 'js_event_wpv_parametric_search_form_updated', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.view_changed_form (object) The jQuery object for the View form after being updated
	* data.view_changed_form_additional_forms_only (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-form-view] shortcode
	* data.view_changed_form_additional_forms_full (object) The jQuery object containing additional forms from other instances of the same View inserted using the [wpv-view] shortcode
	*/
  remove_values = jQuery.unique(jQuery.trim(jQuery('#activity_remove_ids').val()).split(","));
jQuery.each( remove_values, function( i, val ) {
  jQuery("select[name='wpv-relationship-filter[]']  option[value="+val+"]").remove();
});
  

});

Can you please confirm it works as expected now. As there is no such feature available to make it work as expected is real challenge and I think are are near to resolve it.

#2355351

It now works fine for the 2 Activities that had a "Travel ticket discount on activity discount percentage" > 0 in their relation with Travel tickets.

I now changed "Travel ticket discount on activity discount percentage" from 0 to 10 in the relation City Walk-Jungfrau Travel Pass. The City Walk does not pop up though. I'm sure when new values should be noticed? I did clear the Litespeed cache.

#2355469

Minesh
Supporter

Languages: English (English )

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

The thing is that we are fetching all the related intermediary posts where "Travel ticket discount on activity discount percentage is a number lower than or equal 0" that means get all the Activity where "Travel ticket discount on activity discount percentage" field is set to 0.

So, for "City walk Zurich" there are two related post where "Travel ticket discount on activity discount percentage" value for one related post is set to 0 and for another its set to 10. So as we still have one record where "Travel ticket discount on activity discount percentage" is set to 0 (for related post: Montreux Riviera Card) it will return the activity ID "2176" for "City walk Zurich".
=> hidden link

That is why we can not differentiate it as the "City walk Zurich" is set with one related post with discount and with another related post without discount.

I've tried another way, I've modified the JS code added to the content template's JS box:

jQuery(document).ready(function($) {

x = jQuery.trim($('#req_json_str').html());


var tmp_value=jQuery.parseJSON(x);


var newDict = {};

for(var i=0; i<tmp_value.length; i++) {
    newDict[tmp_value[i].id] = tmp_value[i]; 
}

                        

 var options = {};
jQuery.each( newDict , function( i, val ) {

  
                                 
 options += '<option value="' + val.id  + '">' + val.title + '</option>' 

});
                           
                       

jQuery("select[name='wpv-relationship-filter[]']").html(options);

});

I've also added the view as given under to your content template using HTML block:
=> hidden link

[wpv-view name='filter-get-all-activities-ids' cached="off"]

I've also modified the view filter and output:
=> hidden link

Can you please confirm it works as expected.

#2356035

Hi Minesh,

Thanks again. I noticed 2 things this time:
1) After page load, the City Walk is in the bottom of the dropdown. If I click it, it jumps up.
2) If I then click Submit, while City Walk is selected, the output includes the Montreux Card while that one has a percentage of 0, so we would like to not see it in the output.

I may need to rethink this, and try to find a solution that is closer to what Toolset offers. To keep things as simple as possible.

The purpose of the Travel ticket-Activity post relationship is to show users which Activities are discounted to them if they have a specific Travel ticket (discount percentage of the post relationship > 0) AND to show users which Activities are NOT discounted if they have that Travel ticket (discount percentage of the post relationship = 0).

Would it make sense to have two Travel ticket-Activity post relationships? The one I already have would be only for discounted Activities. The other relationship would be for non-discounted Activities and it would not have the "Travel ticket discount on activity discount percentage" field in the intermediate post type. That way I could only use the first relationship in the view we're discussing here. And I could use both relationships in the template for a single Travel ticket, to show both discounted and non-discounted Activities: I'm not sure if I need 2 views for that. All in 1 view would be better; we would not need a filter on "Travel ticket discount on activity discount percentage".

What do you think?

Kind regards,
Arno

#2356053

Minesh
Supporter

Languages: English (English )

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

Would it make sense to have two Travel ticket-Activity post relationships? The one I already have would be only for discounted Activities. The other relationship would be for non-discounted Activities and it would not have the "Travel ticket discount on activity discount percentage" field in the intermediate post type. That way I could only use the first relationship in the view we're discussing here. And I could use both relationships in the template for a single Travel ticket, to show both discounted and non-discounted Activities: I'm not sure if I need 2 views for that. All in 1 view would be better; we would not need a filter on "Travel ticket discount on activity discount percentage".
==>
Yes, that sounds interesting and could help and you should be able to list both dropdowns with single view. I suggest you should give a try to this and check if that helps you to resolve the issue.

#2358281

Hi Minesh,

I made the changes and most of it works. I have one issue left in the view we were working on: I now have a custom search field on the new post relationship for discounted activities. The control type is "select multiple". That works, except that I'd like the view to output travel tickets that have a relation with ALL selected activities, not with ANY of the selected activities. Is there a way to do that?

#2358601

Minesh
Supporter

Languages: English (English )

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

Can you please share what changes you made and where I can see the select fields you added once I review your new setup then I will be able to answer you if what you want is possible or not.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.