Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions includes/classes/Feature/Facets/Types/Taxonomy/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
8 changes: 5 additions & 3 deletions includes/classes/Indexable/Post/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
15 changes: 15 additions & 0 deletions tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}
}