get_objects_in_term

Retrieve object_ids of valid taxonomy and term.

Description

(WP_Error|array) get_objects_in_term( (int|array) $term_ids, (string|array) $taxonomies, (array) $args = array() ); 

The strings of $taxonomies must exist before this function will continue. On failure of finding a valid taxonomy, it will return an WP_Error class, kind of like Exceptions in PHP 5, except you can't catch them. Even so, you can still test for the WP_Error class and get the error message.

The $terms aren't checked the same as $taxonomies, but still need to exist for $object_ids to be returned.

It is possible to change the order that object_ids is returned by either using PHP sort family functions or using the database by using $args with either ASC or DESC array. The value should be in the key named order..

Returns (WP_Error|array)

If the taxonomy does not exist, then WP_Error will be returned. On success. the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.

Parameters (3)

0. $term_ids (int|array)
Term id or array of term ids of terms that will be used.
1. $taxonomies (string|array)
String of taxonomy name or Array of string values of taxonomy names.
2. $args — Optional. (array) => array()
Change the order of the object_ids, either ASC or DESC.

Usage

  1. if ( !function_exists( 'get_objects_in_term' ) ) { 
  2. require_once ABSPATH . WPINC . '/taxonomy.php'; 
  3.  
  4. // Term id or array of term ids of terms that will be used. 
  5. $term_ids = null; 
  6.  
  7. // String of taxonomy name or Array of string values of taxonomy names. 
  8. $taxonomies = null; 
  9.  
  10. // Change the order of the object_ids, either ASC or DESC. 
  11. $args = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_objects_in_term($term_ids, $taxonomies, $args); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/taxonomy.php  
  1. function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! is_array( $term_ids ) ) { 
  5. $term_ids = array( $term_ids ); 
  6. if ( ! is_array( $taxonomies ) ) { 
  7. $taxonomies = array( $taxonomies ); 
  8. foreach ( (array) $taxonomies as $taxonomy ) { 
  9. if ( ! taxonomy_exists( $taxonomy ) ) { 
  10. return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 
  11.  
  12. $defaults = array( 'order' => 'ASC' ); 
  13. $args = wp_parse_args( $args, $defaults ); 
  14.  
  15. $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC'; 
  16.  
  17. $term_ids = array_map('intval', $term_ids ); 
  18.  
  19. $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 
  20. $term_ids = "'" . implode( "', '", $term_ids ) . "'"; 
  21.  
  22. $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"); 
  23.  
  24. if ( ! $object_ids ) { 
  25. return array(); 
  26. return $object_ids;