WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 2 years ago

#23635 new enhancement

get_objects_in_term - identify matching terms

Reported by: leepowers Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.5.1
Component: Taxonomy Keywords: needs-patch needs-unit-tests
Focuses: Cc:

Description

get_objects_in_term is great for viewing taxonomy relationships for non-posts. However the return value is too limited for certain use cases. For When fetching object_ids for multiple terms the return value doesn't inform as to the matching term.

For instance this query:

$object_ids = get_objects_in_term(array(55, 66, 77, 88, 99), "my_custom_taxonomy"); 

Might return an array of object_ids as follows:

array(101, 202, 303, 404); 

However there's no way to tell which term a particular object_id matched. Did object 101 match term 77? Or maybe it was term 99? Or both?

I've created a modified version of get_objects_in_term which will addresses this issue. I'll submit a patch, but am wondering if there's another way to achieve this functionality.

Change History (2)

#1 @boonebgorges
3 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

am wondering if there's another way to achieve this functionality.

Only by looping over each of your terms and calling get_objects_in_term( $term_id ) for each.

I can imagine supporting something like $args['format'] that lets you specify that you'd like the results returned as an array:

$objects = get_objects_in_term( array( 4, 5 ), 'my_taxonomy', array( 'format' => 'array' ) );

/*
array(
    4 => array( 10, 22, 130 ),
    5 => array( 15, 31 ),
),
*/

#2 @chriscct7
2 years ago

  • Severity changed from minor to normal
Note: See TracTickets for help on using tickets.