• Resolved mrloco

    (@mrloco)


    I used this code to hide certain product categories in woocommerce with code snippets.

    add_filter( 'get_terms', 'filter_get_terms', 10, 3 );
    function filter_get_terms( $terms, $taxonomies, $args ) {
        $new_terms = [];
    
        // if a product category and on the shop page
        if ( ! is_admin() ) {
            foreach ( $terms as $term ) {
                if ( ! in_array( $term-> slug, [ 'seinakellad', 'nastennye-chasy', 'wall-clock', 'juuksekammid', 'grebni-dlja-volos', 'hair-combs' ] ) ) {
                    $new_terms[] = $term;
                }
            }
    
            $terms = $new_terms;
        }
    
        return $terms;
    }

    After updating to PHP 8.2 I got this error:
    **
    Warning**: Attempt to read property “slug” on int in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()’d code on line 8

    Can help me to fix it?

    Thanks!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi @mrloco,

    It’s worth noting that the array of $terms that this filter handles might be an array of term IDs instead of term objects, depending on what the output attribute is set to.

    As such, if you want to access properties like the slug then you’ll need to convert the term into a proper WP_Term object before doing so:

    add_filter( 'get_terms', function ( $terms ) {
    	if ( is_admin() ) {
    		return $terms;
    	}
    
    	$new_terms = [];
    	$invalid_slugs = [ 'seinakellad', 'nastennye-chasy', 'wall-clock', 'juuksekammid', 'grebni-dlja-volos', 'hair-combs' ];
    
    	foreach ( $terms as $term ) {
    		$term_object = get_term( $term );
    
    		if ( $term_object && ! is_wp_error( $term_object ) && ! in_array( $term_object->slug, $invalid_slugs ) ) {
    			$new_terms[] = $term;
    		}
    	}
    
    	return $new_terms;
    } );
Viewing 1 replies (of 1 total)

The topic ‘Problem with CSS in PHP 8.2’ is closed to new replies.