Skip to content

Commit 1f73437

Browse files
author
iFlair
committed
Fixed : 64357 - get_terms() no longer returns terms when include or exclude is set to all or an empty value in WP 6.9
1 parent 2929fe2 commit 1f73437

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

src/wp-includes/class-wp-tax-query.php

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -383,87 +383,80 @@ protected function get_sql_for_query( &$query, $depth = 0 ) {
383383
*/
384384
public function get_sql_for_clause( &$clause, $parent_query ) {
385385
global $wpdb;
386-
386+
387387
$sql = array(
388388
'where' => array(),
389389
'join' => array(),
390390
);
391-
391+
392392
$join = '';
393393
$where = '';
394-
394+
395395
$this->clean_query( $clause );
396-
396+
397397
if ( is_wp_error( $clause ) ) {
398398
return self::$no_results;
399399
}
400-
400+
401401
$terms = $clause['terms'];
402402
$operator = strtoupper( $clause['operator'] );
403-
403+
404+
/**
405+
* FIX: Handle WP 6.9 regression
406+
* An empty "include" or "exclude" (or passing "all") must NOT filter terms.
407+
*/
408+
if ( isset( $clause['include'] ) && ( $clause['include'] === 'all' ) ) {
409+
return $sql; // no filtering
410+
}
411+
if ( isset( $clause['exclude'] ) && ( $clause['exclude'] === 'all' ) ) {
412+
return $sql; // no filtering
413+
}
414+
if ( empty( $terms ) ) {
415+
return $sql; // empty = no filtering (correct behavior)
416+
}
417+
418+
// Convert term array to CSV
419+
$terms_csv = implode( ',', array_map( 'intval', (array) $terms ) );
420+
404421
if ( 'IN' === $operator ) {
405-
406-
if ( empty( $terms ) ) {
407-
return self::$no_results;
408-
}
409-
410-
$terms = implode( ',', $terms );
411-
412-
/*
413-
* Before creating another table join, see if this clause has a
414-
* sibling with an existing join that can be shared.
415-
*/
422+
423+
// standard IN logic
416424
$alias = $this->find_compatible_table_alias( $clause, $parent_query );
417425
if ( false === $alias ) {
418426
$i = count( $this->table_aliases );
419427
$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
420-
421-
// Store the alias as part of a flat array to build future iterators.
428+
422429
$this->table_aliases[] = $alias;
423-
424-
// Store the alias with this clause, so later siblings can use it.
425-
$clause['alias'] = $alias;
426-
430+
$clause['alias'] = $alias;
431+
427432
$join .= " LEFT JOIN $wpdb->term_relationships";
428433
$join .= $i ? " AS $alias" : '';
429434
$join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
430435
}
431-
432-
$where = "$alias.term_taxonomy_id $operator ($terms)";
433-
436+
437+
$where = "$alias.term_taxonomy_id IN ($terms_csv)";
438+
434439
} elseif ( 'NOT IN' === $operator ) {
435-
436-
if ( empty( $terms ) ) {
437-
return $sql;
438-
}
439-
440-
$terms = implode( ',', $terms );
441-
440+
442441
$where = "$this->primary_table.$this->primary_id_column NOT IN (
443442
SELECT object_id
444443
FROM $wpdb->term_relationships
445-
WHERE term_taxonomy_id IN ($terms)
444+
WHERE term_taxonomy_id IN ($terms_csv)
446445
)";
447-
446+
448447
} elseif ( 'AND' === $operator ) {
449-
450-
if ( empty( $terms ) ) {
451-
return $sql;
452-
}
453-
448+
454449
$num_terms = count( $terms );
455-
456-
$terms = implode( ',', $terms );
457-
450+
458451
$where = "(
459452
SELECT COUNT(1)
460453
FROM $wpdb->term_relationships
461-
WHERE term_taxonomy_id IN ($terms)
454+
WHERE term_taxonomy_id IN ($terms_csv)
462455
AND object_id = $this->primary_table.$this->primary_id_column
463456
) = $num_terms";
464-
457+
465458
} elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
466-
459+
467460
$where = $wpdb->prepare(
468461
"$operator (
469462
SELECT 1
@@ -475,11 +468,12 @@ public function get_sql_for_clause( &$clause, $parent_query ) {
475468
)",
476469
$clause['taxonomy']
477470
);
478-
471+
479472
}
480-
473+
481474
$sql['join'][] = $join;
482475
$sql['where'][] = $where;
476+
483477
return $sql;
484478
}
485479

0 commit comments

Comments
 (0)