Skip Navigation

[Resolved] custom search view with double query

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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by fahimS-2 1 year, 8 months ago.

Assisted by: Nigel.

Author
Posts
#2568205

Tell us what you are trying to do?
In a custom taxonomy, I have 4 options: cat1, cat2, cat3, cat4. I created a custom search view with the block editor. In the query filter section of content selection of that search view, I added query filter to show only the posts that belong to any of the first 3 categories (cat1, cat2, cat3). Now how can I implement a search filter where users will be able to search by those categories to see cat1 or cat2 or cat3?

#2568345

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Within the UI you cannot add a query filter for something (taxonomy) and also add a custom search filter for the same thing, because the custom search filter adds a query filter for that thing where the value comes from a url parameter (that contains the filter value when the search is applied on the front end).

To achieve what you want you should remove the query filter, and set up the custom search filter as required.

That will include all 4 of your categories, including the one you do not want. Check it is working before continuing.

Now, there are two parts to the solution to this problem.

One is to make sure that no posts with the fourth category are included in the results.

You can do that by using the wpv_filter_query to modify the query arguments for the View. (See https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query)

You need to let the search filter arguments apply, but also prevent posts with the fourth category from being displayed.

Your code will need to look something like this:

add_filter( 'wpv_filter_query', 'ts_mod_tax_query', 101, 3 );
function ts_mod_tax_query( $view_args, $view_settings, $view_id ){
    
    if ( in_array( $view_id, array( 123 ) ) ) { // Edit for View IDs

        $tax_condition = array(
            'taxonomy'  => 'custom-cat',
            'field'     => 'slug',
            'terms'     => 'cat4',
            'operator'  => 'NOT IN'
        );

        if ( isset($view_args['tax_query']) ){
            $view_args['tax_query'][] = $tax_condition;
        } else {
            $view_args['tax_query'] = array($tax_condition);
        }

    }

    return $view_args;
}

(You would need to edit that for the ID of the View this should apply to, the slug of the taxonomy, the slug of the category to exclude. You can add code such as this at Toolset > Settings > Custom Code.)

That will prevent cat4 posts being shown in the results.

But there is still a problem, that cat4 will be shown in the search dropdown.

You can see an example of the kind of code you can use to prevent that here, where you can add cat4 to a blacklist: hidden link

Note, providing custom code is outside the scope of support, so if you are not comfortable adding such code to your site and modifying it to your needs you may need to recruit a developer to do that for you (see https://toolset.com/contractors/).

#2568561

I made the following changes to the code according to my context.

add_filter( 'wpv_filter_query', 'ts_mod_tax_query', 101, 3 );
function ts_mod_tax_query( $view_args, $view_settings, $view_id ){
     
    if ( in_array( $view_id, array( 642 ) ) ) { // Edit for View IDs
 
        $tax_condition = array(
            'taxonomy'  => 'saas-feature', //Custom taxonomy slug
            'field'     => 'slug',
            'terms'     => array('four', 'five'), //exclude two categories
            'operator'  => 'NOT IN'
        );
 
        if ( isset($view_args['tax_query']) ){
            $view_args['tax_query'][] = $tax_condition;
        } else {
            $view_args['tax_query'] = array($tax_condition);
        }
 
    }
 
    return $view_args;
}

Is the code correct for excluding two categories? Or is there a better way that I should implement?

#2568583

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

It looks like it is working to me.

When I visit hidden link there are 3 posts showing (One, Two, and three).

I don't know what terms are assigned to those posts, nor what other posts there are.

But if I go to the saas-feature taxonomy archive for the term four (whose posts we are trying to exclude from the search page) I see that there is a post (also titled "four") on that archive: hidden link

So the post ("four") which has the term four assigned to it is being excluded from the search results.

That's what you wanted, isn't it?

Similarly the post "Five" which has appears to have the term "five" assigned to it is also excluded from the search page, so you seem to have worked out that you need to update the line with the terms argument to an array of all the terms you want to exclude, e.g.

        'terms'     => array('four','five'), //cat4 & cat5 slugs
#2569581

My issue is resolved now. Thank you!