albertoG-9
Support threads created in the last 30 days: 0
Favorite Forum Topics
This user has no favorite topics.
Forum Topics Created
Status | Topic | Supporter | Voices | Posts | Freshness |
---|---|---|---|---|---|
Filter view query with taxonomy and allow users to filter through its terms only
Started by: albertoG-9
in: Toolset Professional Support
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; } |
2 | 6 | 3 months ago |