Make WordPress Core


Ignore:
Timestamp:
10/22/2025 04:28:33 PM (13 months ago)
Author:
johnjamesjacoby
Message:

Query: Prevent querying for all terms in WP_Term_Query when include is set to [0].

This change brings the include parameter of WP_Term_Query up-to-speed with the include and __in parameters of other _Query classes, so that sending a value of [0] will not unintentionally perform an unbound query that returns all Terms.

It also introduces 3 new unit tests (for the Post, Term, and User query classes) to compare this behavior between them and ensure they remain consistent going forward.

Props audrasjb, hareesh-pillai, hellofromTonya, johnjamesjacoby, mukesh27.

Fixes #47719.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-term-query.php

    r60697 r61048  
    471471                $include      = $args['include'];
    472472
    473                 $inclusions = '';
    474473                if ( ! empty( $include ) ) {
    475474                        $exclude      = '';
    476475                        $exclude_tree = '';
    477476                        $inclusions   = implode( ',', wp_parse_id_list( $include ) );
    478                 }
    479 
    480                 if ( ! empty( $inclusions ) ) {
     477
    481478                        $this->sql_clauses['where']['inclusions'] = 't.term_id IN ( ' . $inclusions . ' )';
    482479                }
    … …  
    816813
    817814                if ( empty( $terms ) ) {
     815                        $this->terms = array();
     816
    818817                        if ( $args['cache_results'] ) {
    819                                 wp_cache_set_salted( $cache_key, array(), 'term-queries', $last_changed );
    820                         }
    821                         return array();
     818                                wp_cache_set_salted( $cache_key, $this->terms, 'term-queries', $last_changed );
     819                        }
     820
     821                        return $this->terms;
    822822                }
    823823
Note: See TracChangeset for help on using the changeset viewer.