I'm regularly seeing Apache errors when my category-single-page.php is pulling in the categories related to a post:
PHP Warning: array_values() expects parameter 1 to be array, bool given in /wp-content/themes/my-theme/inc/category-single-page.php on line 4
This is the first few lines from the php file (lines numbered for reference only NOT in the actual file)
<?php while ( have_posts() ) : the_post(); ?>
<?php
$thiscategory = get_the_terms( $post->ID, $taxonomy_name );
$thiscategory = array_values( $thiscategory ); // This is line 4
$section = get_post_type( $post->ID );
$thispost = $post->ID;
?>
The php file doesn't seem to generate an error all of the time it is called - so I think my code is OK (I could be wrong!) so any thoughts of what could be causing Apache to log the error on occasion, or is it my code at fault?
I'm regularly seeing Apache errors when my category-single-page.php is pulling in the categories related to a post:
PHP Warning: array_values() expects parameter 1 to be array, bool given in /wp-content/themes/my-theme/inc/category-single-page.php on line 4
This is the first few lines from the php file (lines numbered for reference only NOT in the actual file)
<?php while ( have_posts() ) : the_post(); ?>
<?php
$thiscategory = get_the_terms( $post->ID, $taxonomy_name );
$thiscategory = array_values( $thiscategory ); // This is line 4
$section = get_post_type( $post->ID );
$thispost = $post->ID;
?>
The php file doesn't seem to generate an error all of the time it is called - so I think my code is OK (I could be wrong!) so any thoughts of what could be causing Apache to log the error on occasion, or is it my code at fault?
Share Improve this question edited Oct 9, 2020 at 19:51 Howdy_McGee♦ 20.9k24 gold badges91 silver badges177 bronze badges asked Oct 9, 2020 at 19:37 SNCookeSNCooke 111 bronze badge 1 |1 Answer
Reset to default 0It's a PHP error, not an Apache error.
What's happening is that get_the_terms()
returns an array of terms, or false
if there are no terms or the post doesn't exist.
You can fix this like this:
$thiscategory = get_the_terms( $post->ID, $taxonomy_name );
if ( is_array( $thiscategory ) ) {
// Only runs if $thiscategory is an array.
$thiscategory = array_values( $thiscategory ); // This is line 4
}
$section = get_post_type( $post->ID );
$thispost = $post->ID;
var_dump( $thiscategory );
and check if it is indeed an array? – Tony Djukic Commented Oct 13, 2020 at 20:56