Skip Navigation

[Resolved] Modify View Filter Query to Check for a Custom Condition

This support ticket is created 2 years, 5 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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by morganS-2 2 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2203093

This page (hidden link) has a view that lists posts of a custom type “Listing” which is created using Toolset.

A Listing post has a “Location” custom field of type “Address”. This field is generally required when a user tries to add a Listing using a frontend Toolset form, but it's optional for other users with a custom role “Partner”, so it can be left empty by them.

There is a distance filter on that Marketplace page which makes calculations based on the “Location” field value and returns Listings accordingly. By default, this filter will return only the Listings with a “Location” field that has a value (not empty field) and located within the selected radius from a specified center. This current behavior of such filter definitely makes sense; however, we want the view to also return Listings posted by Partners, whatever location a listing has or even if it has no location. In other words, we need these listings by partners to be location-neutral; they should respect other filters like Condition, Price, etc., but the distance filter should have no effect on them.

Is there a way to modify the filter query to check the distance filter value only if the Listing is posted by a user who is NOT a Partner, and ignore the filter otherwise?

So, we’re trying here to create a condition with an OR relationship between the distance filter and the listing author role, like this: Check if it’s by partner OR filter by location.

I followed the documentation here (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query) to see how to modify the filter query, but I didn’t know where and how to include our condition here.

#2204563

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I'm afraid, the Toolset (or even WordPress) doesn't support the conditional query filtering that you're planning to use, out-of-the-box.

The closest possible workaround that I can think of would be using two separate views on a page, one for the listings from non-partners and the other for the listings from the partners.

In the non-partner listings view, you'll include all the search fields, including the distance filter, and set the view to update the results without using the AJAX (i.e. the page should reload to show the search results).

In the partner listings view, you'll not include the front-end search fields and only the query filter for the fields other than the address field, so that the URL parameter linked to the query filters are the same as the ones used in the non-partner listing view.

As a result, when the search will be performed using the search fields from the non-partner listings view, the results will also be updated for the partner listings view, but the distance/address field filter will have no effect, on this partner listings view.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2207921

Hello Waqar,

Thank you for your reply.

That was a nice workaround. However, we prefer to have one list (view) for all posts and to keep using AJAX when updating results.

We thought of another workaround and applied it, and it's working fine now. I would like to share it with you:
- Using "wpv_filter_query" hook...
- Get the IDs of all the posts returned with the passed $query_args, which will be considering the distance filter if used.
- Manually get the values of all active filters from the URL, except for the distance filter if any.
- Use the grabbed values in a newly created query arguments array, which compares with Partners posts only.
- Get the IDs of all the posts returned using the new args.
- Merge the two lists of IDs we have now to get a list of IDs of all posts that match our filters, including the Partners posts after ignoring their locations.
- Modify the passed $query_args to use the post IDs list we generated.
- Return $query_args at the end to be passed then to the WP_Query.

Thanks again for your assistance.

#2207923

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.