Make WordPress Core

Opened 4 years ago

#51905 assigned defect (bug)

Issue with 'is_object_in_term' when terms assigned in increseing order

Reported by: hmishra080's profile hmishra080 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Taxonomy Keywords:
Focuses: Cc:

Description

Hello Team,

Today I have encountered one issue in the 'is_object_in_term' function.

Scenario:
I have two terms 'A' and 'B', which have Ids 1 and 2. The logic on my page to show those posts which have term 'A'. I was checking that with the below code.

has_term($term_object, 'taxonomy-name')

But the above code making condition true for term B posts also and to overcome this I have to forcefully change the code with the below one.

has_term($term_id, 'taxonomy-name')

When I looked into it more then I found that $term_object contains 2 integer values in the array i.e. term_id and term_taxonomy_id. For term 'A' the term_id is 1 and term_taxonomy_id is 2 and for term 'B' (2 & 3). So when code flow to 'is_object_in_term' function from 'has_term' and reach the following line of code:

$terms = (array) $terms;

$ints = array_filter( $terms, 'is_int' );
if ( $ints ) {

$strs = array_diff( $terms, $ints );

} else {

$strs =& $terms;

}

So, for term A the value of $ints is array(1, 2) and for term B array(2, 3).

Now when term 'B' set on a post and we check 'has_term' for the term 'A' with 'term_object', the 'has_term' function returns true because of the below code.

If term is an int, check against term_ids only.
if ( $ints && in_array( $object_term->term_id, $ints, true ) ) {

return true;

}

As term 'B' has id 2 and value of $ints for term 'B' is array(2, 3) so, condition true for the term 'B' also.

Please see the attachment for code reference. I hope this makes sense and I am able to point out the issue.

Thanks
Harshad

http://prntscr.com/vtmb35
http://prntscr.com/vtmbo6

Change History (0)

Note: See TracTickets for help on using tickets.