• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / get_objects_in_term() – Retrieve object_ids of valid taxonomy and term.

You appear to be a bot. Output may be restricted

Description

Retrieve object_ids of valid taxonomy and term.

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'.

Usage

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

Parameters

$term_ids
( int|array ) required – Term ID or array of term IDs of terms that will be used.
$taxonomies
( string|array ) required – String of taxonomy name or Array of string values of taxonomy names.
$args
( array|string ) optional – Change the order of the object_ids, either ASC or DESC.

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.

Source

File name: wordpress/wp-includes/taxonomy.php
Lines:

1 to 42 of 42
function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
  global $wpdb;

  if ( ! is_array( $term_ids ) ) {
    $term_ids = array( $term_ids );
  }
  if ( ! is_array( $taxonomies ) ) {
    $taxonomies = array( $taxonomies );
  }
  foreach ( (array) $taxonomies as $taxonomy ) {
    if ( ! taxonomy_exists( $taxonomy ) ) {
      return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
    }
  }

  $defaults = array( 'order' => 'ASC' );
  $args     = wp_parse_args( $args, $defaults );

  $order = ( 'desc' === strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';

  $term_ids = array_map( 'intval', $term_ids );

  $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'";
  $term_ids   = "'" . implode( "', '", $term_ids ) . "'";

  $sql = "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";

  $last_changed = wp_cache_get_last_changed( 'terms' );
  $cache_key    = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed";
  $cache        = wp_cache_get( $cache_key, 'terms' );
  if ( false === $cache ) {
    $object_ids = $wpdb->get_col( $sql );
    wp_cache_set( $cache_key, $object_ids, 'terms' );
  } else {
    $object_ids = (array) $cache;
  }

  if ( ! $object_ids ) {
    return array();
  }
  return $object_ids;
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 25th November 2019

Primary Sidebar

Information

Function name: get_objects_in_term
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/taxonomy.php
File ref: wp-includes/taxonomy.php
Deprecated?: No
API Letters: G,I,O,T

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites