WordPress.org

Make WordPress Core

Opened 9 years ago

Last modified 7 months ago

#23635 new enhancement

get_objects_in_term - identify matching terms

Reported by: leepowers Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.1
Component: Taxonomy Keywords: has-patch has-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 (4)

#1 @boonebgorges
7 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
6 years ago

  • Severity changed from minor to normal

This ticket was mentioned in PR #1185 on WordPress/wordpress-develop by donmhico.


7 months ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

This PR will add $args['format'] which if set to true will format the output of get_objects_in_term().

Trac ticket: https://core.trac.wordpress.org/ticket/23635

#4 @donmhico
7 months ago

Thank you for this ticket @leepowers. I know it's been many years now but I attached a PR that does what you suggested.

Example Usage

<?php
// Default
$objects = get_objects_in_term( array( 4, 3 ), 'my_taxonomy' );
var_dump( $objects );
/*
 * Output
   array(5) {
       [0]=>
       string(2) "21"
       [1]=>
       string(2) "26"
       [2]=>
       string(2) "26"
       [3]=>
       string(2) "28"
       [4]=>
       string(2) "42"
   }
 */

$format_objects = get_objects_in_term( array( 4, 3 ), 'my_taxonomy', array( 'format' => true ) );
var_dump( $format_objects );
/*
 * Output
   array(2) {
       [3]=>
           array(3) {
               [0]=>
               string(2) "21"
               [1]=>
               string(2) "26"
               [2]=>
               string(2) "28"
           }
       [4]=>
           array(2) {
               [0]=>
               string(2) "26"
               [1]=>
               string(2) "42"
           }
   }
 */
Note: See TracTickets for help on using tickets.