[wp-trac] [WordPress Trac] #63256: Unnecessary array_map() call in get_terms() when object_ids is not set

WordPress Trac noreply at wordpress.org
Sun Oct 26 16:24:02 UTC 2025


#63256: Unnecessary array_map() call in get_terms() when object_ids is not set
-------------------------------------+------------------------
 Reporter:  dilipbheda               |       Owner:  audrasjb
     Type:  defect (bug)             |      Status:  reviewing
 Priority:  normal                   |   Milestone:  6.9
Component:  Taxonomy                 |     Version:
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+------------------------

Comment (by westonruter):

 @staurand You're right.

 The `object_ids` param was made agnostic to the object type so that it
 could be used to relate posts as well as users, both of which have numeric
 IDs.  It is running `intval` over all of the object IDs. Additionally, the
 `object_ids` are being ultimately used as part of the SQL here:

 {{{
 $this->sql_clauses['where']['object_ids'] = "tr.object_id IN
 ($object_ids)";
 }}}

 The `tr` table here is `term_relationships` ([https://github.com/WordPress
 /wordpress-develop/blob/9824c6f0280792f5d451eb17fdc3b65eb9839e4b/src/wp-
 admin/includes/schema.php#L85-L91 schema]), where the `object_id` field
 has this type:

 {{{
 object_id bigint(20) unsigned NOT NULL default 0,
 }}}

 So one thing is for sure is that it should be an array of integers.

 So what if someone passes `0` as `object_ids`? That seems to be the
 problem here that you've identified. Passing `array( 0 )` won't be a
 problem, since `empty( array( 0 ) )` is `false`. So with the current
 patch:

 {{{
 get_terms( array( 'object_ids' => 0 ) ); // FAIL
 get_terms( array( 'object_ids' => array( 0 ) ) ); // OK
 }}}

 So it seems we can address the problem with a slight tweak to the patch:

 {{{#!diff
 - if ( 'term_order' === $_orderby && empty(
 $this->query_vars['object_ids'] ) ) {
 + if ( empty( $args['object_ids'] ) && ! is_numeric( $args['object_ids'] )
 ) {
 }}}

 I don't know how common it is to have `term_relationships` rows which have
 an `object_id` set to `0`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63256#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list