Skip Navigation

[Resolved] Front end filter not working as expected

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

Problem:

In a post view, with a taxonomy filter in custom search form:
If I don't check any checkbox, Views will output all posts, including posts which assigned without any term of taxonomy "Members-Arts-Culture", I need only posts with term of taxonomy "Members-Arts-Culture".

Solution:

This is expected result, if you don't check any checkbox, Views will output all posts, including posts which assigned with any term of taxonomy "Members-Arts-Culture".

In your case, it needs some custom PHP codes, for example, when user open the URL:

http://aboutdereham.org/members-arts-culture/

You can use Views filter hook "wpv_filter_query" to trigger an PHP function,

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

in this PHP function check if there is URL parameter "wpv-member-arts-culture", if there isn't, add a taxonomy filter to the query,
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

return posts with which assigned with any term of taxonomy "Members-Arts-Culture"

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 6 years, 6 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/Hong_Kong (GMT+08:00)

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#772914
image-1.jpg

I am building a membership site which allows members to post a listing about their organisation.

I am using a custom post type called 'members' which has three associated Taxonomies:-

'Members-Arts-Culture'
'Members-Clubs-Leisure'
'Members-Community'

The listing of all members, you can see at hidden link
You can see that each taxonomy has a checkbox list which allows you to filter the view. This is working fine.

However when I created a dedicated page for each taxonomy type, I want just the posts containing that taxonomy to appear, then a front end filter for that taxonomy.

So if you look at hidden link
I have set this up using a view which has a front end filter for the taxonomy 'Members-Arts-Culture'.
However this view is still listing all posts from the other two taxonomies (until any one of the checkbox buttons is pressed).
That is the problem.

So the 'Members-Arts-Culture' front end filter in the view is not filtering out the other two taxonomies.

Could you advise how to sort this issue please.

Regards

Robert

#775170

Dear Robert,

This is expected result, if you don't check any checkbox, Views will output all posts, including posts which assigned with any term of taxonomy "Members-Arts-Culture".

In your case, it needs some custom PHP codes, for example, when user open the URL:
hidden link
You can use Views filter hook "wpv_filter_query" to trigger an PHP function,
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

in this PHP function check if there is URL parameter "wpv-member-arts-culture", if there isn't, add a taxonomy filter to the query,
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
return posts with which assigned with any term of taxonomy "Members-Arts-Culture"

#780739

Dear Luo

Thanks for your advice. I have written the function but I'm not sure if it is correct. So let me first confirm what I want to happen:-

The page at hidden link uses a View with the ID 138.
This view has a front end filter for the taxonomy 'Members-Arts-Culture' (as shown in my image I previously uploaded).
Now I want that View to call a Views filter hook "wpv_filter_query" to trigger a PHP function which will allow only posts with the taxonomy 'Members-Arts-Culture' to be passed on to the front end filter.

Here is the function I have written:-

// Used by the view ID138 'members-art-culture' to insert a taxonomy filter
add_filter( 'wpv_filter_query', 'members_arts_culture_func', 99, 3 );
function members_arts_culture_func($query, $setting, $views_ID)
{
    if($views_ID == 138)
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'member-arts-culture',
            'operator' => 'IN'
        );
    }
    return $query;
}

But it is not providing the first filter.

Could you have a look at the code and see if there is an error please?
Do I need to specify the post type perhaps?
I have used the slug name for the taxonomy 'member-arts-culture' (not the plural name 'members-art-culture')

Regards

Robert

#782351

I have tried the URL you mentioned above:
hidden link

But get a 404 error, since it is a custom codes problem, if you need assistance for it, please provide a test site with the same problem, and fill below private detail box with login details and FTP access, also point out the problem page URL and view URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks

#788169

Hi Luo

Sorry about the problem you had accessing the page. It was the live site and I had accidentally put the access post group filter back on. I did some more experimentation and found that the taxonomy filter must have the 'field' and 'terms' included. So you cannot create a filter just by the 'taxonomy' on its own.

So here is the code for two views each of which supports a specific page.

// Used by the view ID138 'members-art-culture' to insert a taxonomy filter
add_filter( 'wpv_filter_query', 'members_arts_culture_func', 99, 3 );
function members_arts_culture_func($query, $setting, $views_ID)
{
    if($views_ID == 138)
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'member-arts-culture',
            'field'    => 'slug',
            'terms'    => array( 'music-provider', 'art-gallery' ),
            'operator' => 'IN'
        );
    }
    return $query;
}

// Used by the view ID143 'members-clubs-leisure' to insert a taxonomy filter
add_filter( 'wpv_filter_query', 'members_clubs_leisure_func', 99, 3 );
function members_clubs_leisure_func($query, $setting, $views_ID)
{
    if($views_ID == 143)
    {
        $query['tax_query'][] = array(
            'taxonomy' => 'member-clubs-leisure',
            'field'    => 'slug',
            'terms'    => array( 'bowling', 'educational-society-club', 'bowling-club-green' ),
            'operator' => 'IN'
        );
    }
    return $query;
}

Thanks for pointing me in the right direction.

Regards

Robert

#801686

You are welcome