Skip Navigation

[Resolved] Filter view query with taxonomy and allow users to filter through its terms only

This thread is resolved. Here is a description of the problem and solution.

Problem:

The customer wants to display posts on a page filtered by the taxonomy "Mitglieder" and allow users to filter the list by the terms of this taxonomy. However, setting the search filters resulted in the page querying all posts when no filters were applied by the user, rather than pre-filtering by the "Mitglieder" taxonomy.

Solution:

We added a custom code snippet to the site's functions.php file using the wpv_filter_query filter. This code pre-filters the query to include only posts that have any term from the "Mitglieder" taxonomy assigned. This initial filtering ensures that the user can use the front-end filter without losing the pre-filtered query. The code targets only the specific view in question, ensuring that posts without any terms assigned from the "Mitglieder" taxonomy are excluded from the results:

add_filter( 'wpv_filter_query', 'filter_mitglieder_taxonomy', 101, 3 );
function filter_mitglieder_taxonomy($query, $setting, $views_ID)
{
    if($views_ID == 1082) // your view ID
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'mitglieder', // taxonomy name
            'field' => 'slug',
            'operator' => 'EXISTS' // Check if any term from the taxonomy is assigned
        );
        $query['tax_query']['relation'] = 'AND';
    }
    return $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.

Our next available supporter will start replying to tickets in about 2.20 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 3 replies, has 2 voices.

Last updated by Mateus Getulio 3 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2727270

Tell us what you are trying to do?
I have several custom post types and several custom taxonomies. I want a page that only shows posts with the taxonomy »Mitglieder« (any of its terms), and I want users to be able to filter this list by the Terms of the Taxonomy »Mitglieder«.
Apparently if I set search filters, I am not able to pre-filter the query anymore (Filter based on the frontend search filter by Mitgliedern). This results in the page querying all posts of the website when all filters are unchecked by the user.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2727587

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hi,

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

To suggest the best way to achieve this, I'll need to see how this view is set up in the admin area.

Can you please share temporary admin login details along with a link to a page where this view can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Mateus

#2728661

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

Thank you for sharing those details, I tried accessing the site but it looks like it is protected with an HTTP basic authentication.

I'm prompted with a user and password request even to browse the site.

I enabled the private fields again for you to share those details with me.

Thank you,
Mateus

#2732296

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I was able to accomplish it with some custom code, I used the wpv_filter_query to filter the query in a way that you're able to use the front end filter afterwards without losing the initial filter.

I added the following code to your theme's functions.php file:

add_filter( 'wpv_filter_query', 'filter_mitglieder_taxonomy', 101, 3 );
function filter_mitglieder_taxonomy($query, $setting, $views_ID)
{
    if($views_ID == 1082) // your view ID
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'mitglieder', // taxonomy name
            'field' => 'slug',
            'operator' => 'EXISTS' // Check if any term from the taxonomy is assigned
        );
        $query['tax_query']['relation'] = 'AND';
    }
    return $query;
}

It targets only the view in the page mitgliederbereich. If the current post has any terms assigned from the taxonomy mitglieder, the posts is shown otherwise it is skipped.

After adding the code above I used the search in the page and it is no longer resetting the taxonomy filter.

Can you please check it and confirm it is working?

Best,
Mateus