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
3 changes: 3 additions & 0 deletions src/wp-includes/class-wp-term-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ protected function populate_terms( $terms ) {
foreach ( $terms as $key => $term_data ) {
if ( is_object( $term_data ) && property_exists( $term_data, 'term_id' ) ) {
$term = get_term( $term_data->term_id );
if ( ! $term instanceof WP_Term ) {
continue;
}
if ( property_exists( $term_data, 'object_id' ) ) {
$term->object_id = (int) $term_data->object_id;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpstan/baselines/property.notFound.neon
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ parameters:
count: 2
path: ../../../src/wp-includes/class-walker-nav-menu.php
-
message: '#^Access to an undefined property WP_Error\|WP_Term\:\:\$object_id\.$#'
message: '#^Access to an undefined property WP_Term\:\:\$object_id\.$#'
identifier: property.notFound
count: 1
path: ../../../src/wp-includes/class-wp-term-query.php
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/term/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,37 @@ public function test_null_child_term_should_not_throw_warning() {
$this->assertEmpty( $q->terms, 'The result should be empty.' );
}

/**
* Tests that a term that no longer resolves is discarded when the query also returns
* object IDs, rather than causing a fatal error.
*
* The error that we should not see:
* `Error: Attempt to assign property "object_id" on null`.
*
* @ticket 59785
*
* @covers WP_Term_Query::populate_terms
*/
public function test_unresolvable_term_with_object_id_should_be_discarded() {
register_taxonomy( 'wptests_tax', 'post' );

$post_id = self::factory()->post->create();
$this->term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
wp_set_object_terms( $post_id, array( $this->term_id ), 'wptests_tax' );

add_filter( 'get_term', array( $this, 'filter_term_to_null' ) );
$q = new WP_Term_Query(
array(
'taxonomy' => 'wptests_tax',
'object_ids' => $post_id,
'fields' => 'all_with_object_id',
)
);
remove_filter( 'get_term', array( $this, 'filter_term_to_null' ) );

$this->assertSame( array(), $q->terms );
}

public function filter_term_to_null( $term ) {
if ( $this->term_id === $term->term_id ) {
return null;
Expand Down
Loading