diff --git a/includes/classes/Feature/Facets/Types/Taxonomy/Renderer.php b/includes/classes/Feature/Facets/Types/Taxonomy/Renderer.php index 983128543..e50b1d8b2 100644 --- a/includes/classes/Feature/Facets/Types/Taxonomy/Renderer.php +++ b/includes/classes/Feature/Facets/Types/Taxonomy/Renderer.php @@ -72,10 +72,9 @@ public function render( $args, $instance ) { $this->display_count = $instance['displayCount']; if ( ! is_search() ) { + $post_type = Utils\get_post_types_for_tax_query(); - if ( is_tax() ) { - $post_type = get_taxonomy( get_queried_object()->taxonomy )->object_type; - } else { + if ( empty( $post_type ) ) { $post_type = $wp_query->get( 'post_type' ) ? $wp_query->get( 'post_type' ) : 'post'; } diff --git a/includes/classes/Indexable/Post/QueryIntegration.php b/includes/classes/Indexable/Post/QueryIntegration.php index 442866575..c4b84053b 100644 --- a/includes/classes/Indexable/Post/QueryIntegration.php +++ b/includes/classes/Indexable/Post/QueryIntegration.php @@ -243,11 +243,13 @@ public function get_es_posts( $posts, $query ) { } /** - * If not search and not set default to post. If not set and is search, use searchable post types + * If not search and not set, default to post. If not set and is search, use searchable post types. */ if ( empty( $query_vars['post_type'] ) ) { - if ( $query->is_tax() && $query->get_queried_object() ) { - $query_vars['post_type'] = get_taxonomy( $query->get_queried_object()->taxonomy )->object_type; + $tax_post_type = Utils\get_post_types_for_tax_query( $query ); + + if ( ! empty( $tax_post_type ) ) { + $query_vars['post_type'] = $tax_post_type; } elseif ( empty( $query_vars['s'] ) ) { $query_vars['post_type'] = 'post'; } else { diff --git a/includes/utils.php b/includes/utils.php index b8d408d16..a6df74dc7 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -930,3 +930,40 @@ function is_top_level_admin_context() { $is_network = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK; return $is_network ? is_network_admin() : is_admin(); } + +/** + * Safely resolve the post type(s) for a taxonomy query. + * + * Guards against null queried objects, missing taxonomy properties, + * deregistered taxonomies, and non-post-type object types that would + * otherwise cause unexpected behavior when chaining + * get_queried_object()->taxonomy through get_taxonomy(). + * + * @since 5.3.3 + * + * @param \WP_Query|null $query Optional. WP_Query instance. Defaults to the global query. + * @return array Registered post type names on success, empty array otherwise. + */ +function get_post_types_for_tax_query( ?\WP_Query $query = null ): array { + global $wp_query; + + if ( null === $query ) { + $query = $wp_query; + } + + if ( ! $query instanceof \WP_Query || ! $query->is_tax() ) { + return []; + } + + $queried_object = $query->get_queried_object(); + if ( ! $queried_object || ! isset( $queried_object->taxonomy ) ) { + return []; + } + + $taxonomy_object = get_taxonomy( $queried_object->taxonomy ); + if ( ! $taxonomy_object || ! is_array( $taxonomy_object->object_type ) ) { + return []; + } + + return array_values( array_filter( $taxonomy_object->object_type, 'post_type_exists' ) ); +} diff --git a/tests/php/TestUtils.php b/tests/php/TestUtils.php index 79f804c13..f2f0c8777 100644 --- a/tests/php/TestUtils.php +++ b/tests/php/TestUtils.php @@ -623,4 +623,19 @@ public function test_is_top_level_admin_context() { $this->assertTrue( Utils\is_top_level_admin_context() ); } } + + /** + * Test get_post_types_for_tax_query does not error when is_tax is true + * but the queried object is invalid. + * + * @since 5.3.3 + * @group utils + */ + public function test_get_post_types_for_tax_query_invalid_queried_object() { + $query = new \WP_Query(); + $query->is_tax = true; + $query->queried_object = new \stdClass(); + + $this->assertSame( [], Utils\get_post_types_for_tax_query( $query ) ); + } }