Skip Navigation

[Closed] using the same post relationship filter multiple times, AND the resulting posts

This support ticket is created 3 years, 9 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#2000733

Tell us what you are trying to do?
I am trying to use 3 instances of a post-relationship filter, services, to display filtered content. the 3 filters work, except the 3 filters AND the criteria instead of OR the criteria.

Is there any documentation that you are following?
https://toolset.com/forums/topic/creating-a-multi-condition-andor-custom-filter-in-a-view/

I'm trying to follow this documentation, but make it only work on my specific filters, but this seems to affect all the filters.

#2000807

Hello, I need a bit more information about this View to be able to offer more guidance.
- Can you share a URL and describe where I can see the View filters/results on this page of your site?
- Can you share the PHP code you have implemented so far, if any?
- Can you open all the Query Filter panels for this View and take a screenshot so I can see the configurations? In the legacy editor, if you cannot find the Query Filter panel in the main View editor screen, scroll up to the top right corner and click "Screen Options" to activate the Query Filter panel. If you are using the Block Editor to design the View, you must select the top-level View block, then open the Custom Search block configurations to find the Query Filter panel. In either case, please toggle all the Query Filters open and take a screenshot or screenshots so I can see the details of all filters. Include these screenshots in your next reply.

#2000837
filter_problem2.PNG
filter_problem1.PNG

here is the URL: hidden link
that leads right to the page with the filters on it.

I actually have 2 different post relationship filters. The first one is the wpv-relationship filter you see in the query filter.
The second post-relationship filter uses the following code to apply it on the site

// creating a second filter function for many to many relationships between psychologists and services
add_filter( 'wpv_filter_query', 'filter_by_parent_services_func',999,3);
function filter_by_parent_services_func( $query_args, $view_settings, $view_id ) {
   if ( $view_id ==4860  && isset($_GET['wpv-relationship-filter'][0]) && $_GET['wpv-relationship-filter'][0] != 0) {
        if(!isset($query_args['toolset_relationships'])){
            $query_args['toolset_relationships'] = array(
                array(
                'role' => 'parent',
                'related_to' => $_GET['wpv-relationship-filter'][0],
                'relationship' => 'psychologist-therapy-format'
                )
      );
        }
        }
    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'] == 4860){
        $attributes[] = array(
            'query_type'=> 'posts',
            'filter_type'=> 'post_relationship',
            'value'=> 'custom_field_value',
            'attribute'=> 'wpv-relationship-filter',
            'expected'=> 'number',
        );
    }
        return $attributes;
}

I took out the second filter and implemented the first post-relationship filter 3 times in the same format as with the services filter, and it still gave me the same problem of the results 'OR' the 3 filter options.

The following is the last bit of code I've tried:

//change relationship of filters to OR
add_filter( 'wpv_filter_query', 'fix_custom_and_or_query' , 99, 3);
function fix_custom_and_or_query( $query_args, $view_settings, $view_id ) {
$metaQuery = [];

if ( $view_id ==4860  && isset($_GET['wpv-services-filter'][0])){

$meta_query_args = array(
    'relation' => 'OR', // Optional, defaults to "AND"
    array(
        'key'     => '_my_custom_key',
        'value'   => 'Value I am looking for',
        'compare' => '='
    )
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$query_args['meta_query'] = $metaQuery;
}
return $query_args;
}
#2001021

Okay first, let me confirm the setup here. It looks like the /psychologists page is an archive, not a Page. I assume you have created a custom search View in Toolset > Views, and inserted it in this archive using a shortcode or some other element in BB. In other words, this is not a custom search WordPress Archive created in Toolset > WordPress Archive, it is a custom search View displayed inside an archive. Is that correct? I ask because the wpv_filter_query filter does not apply to custom search Archives created in Toolset > WordPress Archives, there are no Toolset filters available for filtering archive results like this. You would have to create a custom search View and place it inside an archive to manipulate the results programmatically with our filters like wpv_filter_query.

Next, let me confirm I understand the desired filters and functionality. I can see you have two custom field filters - Languages and
Waitlist Status. I see you have one taxonomy filter - Location. Those are all pretty clear. Then you have 4 post relationship filters for the Psychologist Therapy Format relationship. I'm a bit confused about how these should all work together.

1. If you want to filter by the Services post relationship, why are there 4 separate single-select filters? Why not include one multiple-select filter, where the User can select one or more Services to use in the filter? Example multi-select field with relationship between Posts and Books:

[wpv-control-post-relationship ancestors="post@book-post.child" url_param="wpv-relationship-filter"]
<div class="form-group">
	<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]Posts[/wpml-string]</label>
	[wpv-control-post-ancestor type="multi-select" ancestor_type="post@book-post.child"]
</div>
[/wpv-control-post-relationship]

2. Can you describe in more detail how you want to combine filters using AND/OR by grouping clauses with parentheses? For example, do you want one of these combination options, or something else?
- ( Parent Coaching AND Individual Therapy AND Career Counseling ) AND ( English ) AND ( Accepting New Patients AND Downtown Montreal )

- ( Parent Coaching OR Individual Therapy OR Career Counseling ) OR ( English ) OR ( Accepting New Patients AND Downtown Montreal)

- ( Parent Coaching AND Individual Therapy AND Career Counseling) OR ( English ) OR ( Accepting new Patients OR Downtown Montreal)

3. In the taxonomy filter, you have selected "Select posts with taxonomy Areas Served in one of these: Downtown Montreal, Eastern Montreal, Online, West-Island" but there is also a front-end filter for this taxonomy. Can you explain the intention here? If you want to allow the User to select the taxonomy using the front-end filter control, you must select the filter configuration that responds to a URL parameter. If you want to predefine the filter, so that the results are only from one of those 4 taxonomy terms, and you do not want the User to change it, you can leave the Query Filter as it is now and delete the front-end control for this filter. If you want to combine these features, allowing the User to select the taxonomy term filter on the front-end, but also limiting the options to only those 4 terms, you need to choose the option that responds to a URL parameter and replace the shortcode with a wpv-control shortcode so you can specify the options manually.
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-control
While working on the post relationship filter, I suggest you use the default wpv-control-post-taxonomy shortcode with all options. Once the post relationship filter is working as expected, come back to this filter for more customization.

#2002227

So for the set up. The filter is in a custom search view, which is displayed in an archive page generated with BB, and displayed through an HTML module calling the custom view.

For the functionality:
1: the multi-select tool isn’t the feel of the search we want. We were hoping to let people keep the first one or two searches clearly listed which are their 1 or 2 most important criteria. Then they could change their third, least important criteria to see what the different results would be. Hence why we want 3 separate select filters for the services option.

2 the desired effect of all filters is to AND all queried filters options, so as more filters are selected, the resulting list shortens to only contain results with all filtered criteria.

3 I have changed the taxonomy filter to respond to the URL parameter. You are right that was better.
Thank you for looking into this further

#2003587

Okay thanks, see my comments below.

So for the set up. The filter is in a custom search view, which is displayed in an archive page generated with BB, and displayed through an HTML module calling the custom view.
Got it, that sounds very similar to what I assumed. You will be able to use wpv_filter_query effectively in this setup.

1: the multi-select tool isn’t the feel of the search we want...Hence why we want 3 separate select filters for the services option.
Okay I understand. My concern is that the system isn't designed to support multiple separate inputs like this for a single post relationship, and there is no public JavaScript API for search inputs. That means the custom multiple inputs design and implementation isn't supported here in the forums, and for the most part you'll be on your own to figure it out. It also means that some features of custom search Views will be lost - most importantly the "show only available options for each input" feature that limits the options in each field based on the selections in other fields. It means that you'll have to show all the options for each input field, which means Users will be able to make filter selections that may lead to "No results found" frequently.

If you plan to try implementing this custom filter system, I suggest you start out with a more default setup during development to confirm the results are what you expect to see. I'll be able to continue assisting you in this scenario, since the filtering system is more standard. Once the filtered results accuracy is confirmed, you can begin replacing the defaults with your own custom individual filters. I would start out by commenting out any wpv_filter_query code, commenting out any wpv_filter_register_url_parameters_for_posts code, deleting the current post relationship Query Filter, and then deleting the existing front-end post relationship controls (i.e. all of the wpv-control-post-relationship shortcodes and everything inside them). I would also turn off the option to "show only available options for inputs" in the View editor screen (In the Custom Search panel choose "Let me choose individual settings manually"). That feature will produce unexpected results when the search form includes custom filters, so it's best to go ahead and disable it now. Then I would insert one post relationship filter control for the Psych Therapy Formats relationship in the Search and Pagination editor. If you start by adding the filter in the Search and Pagination editor, the system will produce the corresponding post relationship Query Filter automatically, configured to respond to the proper URL params. Use the multi-select input filter to test the View's results when combined with custom field and taxonomy filter selections.

I don't think the wpv_filter_register_url_parameters_for_posts filter is necessary here. In the other ticket, this filter is used to tell the system to expect some URL parameters that were not registered in Query Filters. But in your case, I expect you will not be adding any unexpected URL parameters. The post relationship Query Filter will have the correct URL params registered when you create the multiple select field I mentioned earlier. You will be able to see how the system expects to see those selected post IDs reflected in the URL parameters during testing with the default multi-select field. You should aim to replicate that expected URL param pattern when you implement custom filters, rather than trying to implement something fully custom here with new URL params, in my opinion. Piggy-back off the expected URL param structure instead.

2 the desired effect of all filters is to AND all queried filters options, so as more filters are selected, the resulting list shortens to only contain results with all filtered criteria.
Okay I see, let's try to get the multiple select post relationship filter implemented first, then we can use the Toolset Views Debug panel to see how everything is tied together in the resulting query. Then we can discuss using wpv_filter_query to adjust "OR"s to "AND"s where possible.

You can turn on the Views debug panel in Toolset > Settings > Front-end Content, then reload the page on the front-end of the site. You may need to temporarily disable your popup blocker or whitelist this site to see the debug popup appear.

3 I have changed the taxonomy filter to respond to the URL parameter.
Okay great.

The topic ‘[Closed] using the same post relationship filter multiple times, AND the resulting posts’ is closed to new replies.