I've been using the custom code provided by toolset support in this support topic: https://toolset.com/forums/topic/filtering-a-view-by-category-on-frontend-when-the-query-filter-is-also-category/
I changed the taxonomy name from media_category to media_categories and now I'm getting the following PHP Errors in my server log:
[23-Mar-2022 06:03:14 UTC] PHP Notice: Trying to get property 'ID' of non-object in /home/pdccpanel/public_html/wp-content/themes/PDC-Divi-child-theme/functions.php on line 128
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: view_id in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 31
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: pages in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 40
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: view_id in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 31
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: pages in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 40
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: view_id in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 31
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: pages in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 40
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: view_id in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 31
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: pages in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 40
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: view_id in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 31
[23-Mar-2022 06:03:14 UTC] PHP Notice: Undefined variable: pages in /home/pdccpanel/public_html/wp-content/toolset-customizations/toolset-custom-code.php on line 40
This is the code currently on my site:
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
// Filter view results
add_filter('wpv_filter_query','func_filter_view_by_tax',10,3);
function func_filter_view_by_tax($query_args, $settings, $view_id){
if($view_id == 3284318 and ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )){
$query_args['tax_query'][] = array(
'taxonomy' => 'media_categories', // taxonomy name
'field' => 'slug',
'terms' => array('annual-reports','fact-sheets','national-assessments','white-papers'),
'operator' => 'IN'
);
}
return $query_args;
}
// Remove 'ndpba', 'final-report','pdc-fact-sheet' options from view's filter dropdown
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
global $post;
// 1. pages where form (or search View) is inserted
$view_id == 3284318;
// 2. which taxonomy (slug)
$taxonomy = 'media_categories';
// 3. add terms to blacklist *OR* whitelist, not both
$blacklist = array();
$whitelist = array('annual-reports','fact-sheets','national-assessments','white-papers');
if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {
if ( !empty( $blacklist ) ) {
foreach( $terms as $key => $term ){
if ( in_array( $term->slug, $blacklist ) ) {
unset($terms[$key]);
}
}
} elseif ( !empty( $whitelist ) ) {
foreach( $terms as $key => $term ){
if ( !in_array( $term->slug, $whitelist ) ) {
unset($terms[$key]);
}
}
}
}
return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );
I appreciate any insights you have,
- Luigi