Skip Navigation

[Closed] PHP error from Toolset Custom Code

This support ticket is created 2 years, 10 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.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 2 years, 10 months ago.

Author
Posts
#2324657

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

#2324715

Nigel
Supporter

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

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

I think the problem is in the second function, the line immediately after this comment

// 1. pages where form (or search View) is inserted

You currently have

    $view_id == 3284318;

The first problem is that you are comparing ("==") not assigning ("="), and the variable $view_id is not defined.

The second problem is that you shouldn't be defining $view_id anyway, because later you use is_page($pages) and $pages has not been defined anywhere. I think you mean to assign a number to $pages, not to $view_id.

So that line should be

    $pages = 123;

where 123 is the ID of the page where your View (with id 3284318) is located. You could also provide the page slug (in quotes) rather than the ID, the is_page function accepts either (https://developer.wordpress.org/reference/functions/is_page/)

The topic ‘[Closed] PHP error from Toolset Custom Code’ is closed to new replies.