Advertisements

get_objects_in_term

Definition:
function get_objects_in_term( $term_ids, $taxonomies, $args = array() {}

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.

Parameters

  • int|array $term_ids: Term id or array of term ids of terms that will be used
  • string|array $taxonomies: String of taxonomy name or Array of string values of taxonomy names
  • array|string $args: Change the order of the object_ids, either ASC or DESC

Return values

returns: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 code

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

	extract( $args, EXTR_SKIP );



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



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



	$taxonomies = "'" . implode( "', '", $taxonomies ) . "'";

	$term_ids = "'" . implode( "', '", $term_ids ) . "'";



	$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");



	if ( ! $object_ids )

		return array();



	return $object_ids;

}

1508

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: