get_objects_in_term() WordPress Function

The get_objects_in_term() function retrieves the objects in a given term. This function is useful for getting a list of objects in a given term, for example, a list of posts in a given category.

get_objects_in_term( int|int[] $term_ids, string|string[] $taxonomies, array|string $args = array() ) #

Retrieves object IDs of valid taxonomy and term.


Description

The strings of $taxonomies must exist before this function will continue. On failure of finding a valid taxonomy, it will return a WP_Error.

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 are returned by using $args with either ASC or DESC array. The value should be in the key named ‘order’.


Top ↑

Parameters

$term_ids

(int|int[])(Required)Term ID or array of term IDs of terms that will be used.

$taxonomies

(string|string[])(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.

Default value: array()


Top ↑

Return

(string[]|WP_Error) An array of object IDs as numeric strings on success, WP_Error if the taxonomy does not exist.


Top ↑

More Information

If an object is in more than one of the terms passed to $terms, the results returned will contain duplicate object_ids.


Top ↑

Source

File: wp-includes/taxonomy.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More